title: Populating A DataGridView Using SQL Query String With Where Clause Gives A Null Reference Error in C#
body: <p>I am trying to populate a DatagridView with a SQL query string. My Code is below.</p>
  
  <pre><code>   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);
                }
            }
  </code></pre>
  
  <p>This query will retrieve the data according to the value passed from the @DataId parameter. With that result the DataGridView will be populated.</p>
  
  <p>I am getting an error as the below.</p>
  
  <p><a href="https://i.stack.imgur.com/eRLqC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eRLqC.png" alt="enter image description here"></a></p>
  
  <p>It seems that one of my declarations are not capturing correctly. So that this error occurs. I cannot understand what I have done wrong.</p>
  
  <p>Thanks and regards,
  Chiranthaka</p>