ASP.NET C# Must declare the scalar variable

后端 未结 6 1765
一生所求
一生所求 2021-01-17 23:56

I am trying to populate a GridView using a method called PopulateGrid() (below) but keep getting the same server error "Must Declare the scalar variable "@QUALID&q

相关标签:
6条回答
  • 2021-01-18 00:30

    This:

    cmd.Parameters.Add(new SqlParameter("QUALID", val));
    

    should be this:

    cmd.Parameters.Add(new SqlParameter("@QUALID", val));
    

    Sorry, typed too quick, try:

    cmd.Parameters.AddWithValue("@QUALID", val);
    

    OK, you have a slightly more fundamental issue in your code. You create a command object, but then you pass the SQL string and the connection for the command into your dataadapter, where it will execute your sql string with no parameters on it's connection.

    I haven't used dataadapters too much, but I think you need to set the parameters on the select command of your adapter.

    0 讨论(0)
  • 2021-01-18 00:31

    change

    cmd.Parameters.Add(new SqlParameter("QUALID", val));
    

    to either

    cmd.Parameters.Add(new SqlParameter("@QUALID", val));
    

    or

    cmd.Parameters.Add("@QUALID", SqlDbType.WhatFitsYourDB).Value = val; 
    

    and you should be good to go. Your problem is that you are missing a '@' in the paramter name

    0 讨论(0)
  • 2021-01-18 00:32
    String val = TextBox2.Text;
    
    String sql = "SELECT QLEVELNAME FROM Qual_Levels WHERE QUALID=@QUALID";
    SqlCommand cmd = new SqlCommand(sql, new SqlConnection(ConfigurationManager.ConnectionStrings["RecruitmentDBConnString"].ConnectionString));
    SqlDataAdapter da = new SqlDataAdapter(sql, cmd.Connection);
    DataSet ds = new DataSet();
    cmd.Parameters.Add(new SqlParameter("@QUALID", val));
    
    da.SelectCommand = cmd;
    cmd.Connection.Open();
    
    da.Fill(ds, "Qual_Levels");
    
    
    SelectionGrid.DataSource = ds;
    SelectionGrid.DataBind();
    
    ds.Dispose();
    da.Dispose();
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    

    use dis one it will work...(da.selectcommand = cmd;)

    0 讨论(0)
  • 2021-01-18 00:36

    Try adding the @ to your sql param like so

     cmd.Parameters.Add(new SqlParameter("@QUALID", val));
    
    0 讨论(0)
  • 2021-01-18 00:39

    You are missing the "@" where you add the parameter:

    SqlParameter("@QUALID", val)
    
    0 讨论(0)
  • 2021-01-18 00:50

    If using dropdown list you need to add selected value inside :

    SelectedValue='<%# Bind ("QUALID") %>'
    
    0 讨论(0)
提交回复
热议问题