ASP.NET C # Must declare a scalar variable

I am trying to populate the GridView using the PopulateGrid () method (see below), but keep getting the same server error "Must declare scalar variable" @QUALID ".

public void PopulateGrid()
    {
        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));

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

        cmd.Connection.Open();


        SqlDataAdapter da = new SqlDataAdapter(sql, cmd.Connection);

        DataSet ds = new DataSet();
        da.Fill(ds, "Qual_Levels");


        SelectionGrid.DataSource = ds;
        SelectionGrid.DataBind();


        ds.Dispose();
        da.Dispose();
        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }

GridView is declared like this.

<asp:GridView ID="SelectionGrid"
            autogeneratecolumns="False" 
            runat="server" CellPadding="4" 
            ForeColor="#333333" GridLines="None" DataKeyNames="QUALID">

            <Columns>
                <asp:BoundField DataField="QLEVELNAME" HeaderText="Level Name" 
                    ReadOnly="True" SortExpression="name" />
            </Columns>
</asp:GridView>

After trying countless things and going through the forums, I continue to encounter the same error.

Server error in application "/".

Must declare the scalar variable "@QUALID".

Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for error information and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: The scalar variable "@QUALID" must be declared.

Source Error:

Line 282: DataSet ds = new DataSet ();

283: da.Fill(ds, "Qual_Levels" );

- , !

+5
5

:

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

:

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

, , :

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

, . , SQL , sql - .

dataadapters, , .

+3

@ sql param

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

"@", :

SqlParameter("@QUALID", val)
+1

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

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

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

. , "@"

+1
source
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 this one, it will work ... ( da.selectcommand = cmd;)

+1
source

All Articles