1
Reply

Problem in filling datagridview

Nishant Pawar

Nishant Pawar

Jun 12 2010 12:21 PM
2.8k

Hi,

I have designed the database in which there are two columns named Class and roll no.  Now I want to retrieve all the roll no with a specific class. i will enter the class in the textbox and will press the button.I expect that all the Records which satisfy my query must be displayed in data grid view.
i have written the following code for the above but it has some error.

private void button1_Click(object sender, EventArgs e)
   {
    // database name is "nish"

   SqlConnection con = new SqlConnection("data source=(local)\\SQLEXPRESS;initial catalog=nish;integrated security=true");
   SqlDataAdapter sda;
   DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand();

   con.Open();
   cmd.Connection = con;
   string b = textBox1.Text;
   //MessageBox.Show(b); 
   string qry = "select * from tab where class="+b;
   //MessageBox.Show(qry);
   sda = new SqlDataAdapter(qry, con);
   sda.Fill(ds);
   dataGridView1.DataSource = ds.Tables[0];
            
}


 if anybody can find out the problem in my prog. or has some different to my solution please reply as soon as possible.
thanks in advance.

Answers (1)