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.
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