Harika Putta Harika Putta - 2 months ago 9
ASP.NET (C#) Question

Must declare the scalar variable "@CID"

I'm trying to get the data of a user from my database in a loggedin from and for that I'm getting the loginID from the login form textbox and placed into a label.

protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
string CID = Label1.Text;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string CID = Label1.Text;
string ConStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SMSCon"].ConnectionString;
SqlConnection con = new SqlConnection(ConStr);
SqlCommand cmd = new SqlCommand("select StudentID, StudentName, StudentClass, StudentGender, StudentDob, StudentFatherName, StudentPhone, StudentAddress, StudentLogin, StudentPassword from TblStudent where StudentLogin=@CID", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}

Answer
SqlCommand cmd = new SqlCommand("select StudentID, StudentName, StudentClass, StudentGender, StudentDob, StudentFatherName, StudentPhone, StudentAddress, StudentLogin, StudentPassword from TblStudent where StudentLogin=@CID", con);
cmd.Parameters.AddWithValue("@CID",CID);

You need to add @CID parameter to SQL command.