必须声明标量变量“@CID”

时间:2016-10-07 16:10:53

标签: asp.net sql-server webforms

我正在尝试从我的数据库中获取用户的数据,因为我从登录表单文本框中获取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 个答案:

答案 0 :(得分: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命令。