Необходимо объявить скалярную переменную «@CID»

#asp.net #sql-server #webforms

#asp.net #sql-сервер #веб-формы

Вопрос:

Я пытаюсь получить данные пользователя из своей базы данных в loggedin, и для этого я получаю LoginID из текстового поля формы входа и помещаю его в ярлык.

  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();
    }
  

Ответ №1:

 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);
  

Вам нужно добавить параметр @CID в команду SQL.