1
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;
}           
1
  • Just curious. Is there a reason you aren't using something like EntityFramework? Commented Jul 25, 2013 at 6:50

4 Answers 4

2

you have to Add parameter first before assign value of parameter

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;
Sign up to request clarification or add additional context in comments.

Comments

1

Change you code as, You have not added parameter to command parameter collection.

DA.SelectCommand.Parameters.AddWithValue("@FirstName", txtName.Text.Trim());

Above perform both

Comments

1

You should use

DA.SelectCommand.Parameters.Add("@FirstName", txtName.Text.Trim());

Comments

1

Try following:

var paramFName = e.Command.CreateParameter("@FirstName", (txtName.Text).Trim());
e.Command.Parameters.Add(paramFName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.