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
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.
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
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;
)
Try adding the @ to your sql param like so
cmd.Parameters.Add(new SqlParameter("@QUALID", val));
You are missing the "@" where you add the parameter:
SqlParameter("@QUALID", val)
If using dropdown list you need to add selected value inside :
SelectedValue='<%# Bind ("QUALID") %>'