0

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?

Liam
  • 27,717
  • 28
  • 128
  • 190
Saima Maheen
  • 134
  • 3
  • 4
  • 11

2 Answers2

1

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

spajce
  • 7,044
  • 5
  • 29
  • 44
NiteshG86
  • 85
  • 1
  • 8
0
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();
 }