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]; }