private void txtName_KeyDown(object sender, KeyEventArgs e)
{
    SqlDataAdapter DA = new SqlDataAdapter("Search_Student",
        DBConnection.GetConnection());
    DA.SelectCommand.CommandType = CommandType.StoredProcedure;
    DA.SelectCommand.Parameters["@FirstName"].Value = (txtName.Text).Trim();

    DataTable DA1 = new DataTable();
    DA.Fill(DA1);
    dataGridView1.DataSource = DA1;
}

最佳答案

您必须先添加参数,然后再分配参数值

SqlDataAdapter DA = new SqlDataAdapter("Search_Student",
        DBConnection.GetConnection());
    DA.SelectCommand.CommandType = CommandType.StoredProcedure;


     SqlParameter param  = new SqlParameter();
     param.ParameterName = "@FirstName";
      param.Value  = txtName.Text;


    DA.SelectCommand.Parameters.Add(param);

    DataTable DA1 = new DataTable();
    DA.Fill(DA1);
    dataGridView1.DataSource = DA1;

09-04 06:21