Reputation: 132
I have a table with ID
and a Name
column i want to show the one ID
and Name
in a DataGridView
For Example in Textbox
when i enter ID
and click search button the datagridview shows the specifc record. In Search Button
I have following Code but it gives error
con.Open();
Sqlcommand cmd = new Sqlcommand("Select * from Registration where ID = '"+textBox1.Text+"'")
sqldatareader reader = cmd.Executereader();
if (reader.HasRows)
{
datagridview1.DataSource = reader.GetSqlValues()
}
How can i bind the data to datagridview?
Upvotes: 0
Views: 17853
Reputation: 1
con.Open();
Sqlcommand cmd = new Sqlcommand("Select * from Registration where ID= '"+textBox1.Text+"'",con);
sqldatareader reader = cmd.Executereader();
if (reader.HasRows)
{
datagridview1.DataSource = reader.GetSqlValues();
}
Upvotes: 0
Reputation: 85
Instead of data reader use dataSet
:
con.Open();
Sqlcommand cmd = new Sqlcommand("Select * from Registration where ID = '"+textBox1.Text+"'");
sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = cmd;
DataSet ds = new DataSet();
sqlDataAdapter1.Fill(ds);
datagridview1.DataSource = ds.table(0);
This will help you
Upvotes: 1