-1

I am trying to populate a DatagridView with a SQL query string. My Code is below.

   private void btnSrcDataID_Click(object sender, EventArgs e)
        {
            try
             {
                dgvInsertInfo.Refresh();

                SqlComm.Connection = SqlConn;

                String sqlQueryString = ("SELECT * FROM MyDataTable WHERE      
                DataID=@DataID");
                SqlComm.Parameters.AddWithValue("@DataID", txtDataID.Text);

                SqlComm = new SqlCommand(sqlQueryString, SqlConn);
                SqlDataTable = new DataTable();

                SqlAdapt = new SqlDataAdapter(SqlComm);

                DataSet dsQryDataId = new DataSet();
                SqlAdapt.Fill(dsQryDataId);

            //Passing data to DatagridView
                dgvInsertInfo.DataSource = dsQryDataId;

            }
             catch (Exception ex)
              {
                 MessageBox.Show(ex.Message);
              }
          }

This query will retrieve the data according to the value passed from the @DataId parameter. With that result the DataGridView will be populated.

I am getting an error as the below.

enter image description here

It seems that one of my declarations are not capturing correctly. So that this error occurs. I cannot understand what I have done wrong.

Thanks and regards, Chiranthaka

ChiranSJ
  • 195
  • 1
  • 3
  • 20
  • 1
    That's not the query giving this error. You need to set an object to `new`. Might be this... `SqlComm.Connection = SqlConn;`, you may need it to be `SqlComm.Connection = new SqlConn;` Walk through the logic or set a break point to follow it. – SS_DBA Jul 18 '17 at 17:38
  • Have you tried putting a breakpoint and inspecting the value of txtDataID.txt - and then manually run your SP with that passed as a parameter value? Also, did you call `dgvInsertInfo.DataBind();`? – Stan Shaw Jul 18 '17 at 17:39
  • SqlComm might not exist before you use SqlComm.Parameters.AddWithValue since it is constructed after – user6144226 Jul 18 '17 at 17:40

1 Answers1

1

Please include declare @DataID as int

manu vijay
  • 367
  • 3
  • 13