Wednesday, March 9, 2011

Search or View Data using SqlDataAdapter

private void btnSearch_Click(object sender, EventArgs e)
{
try
{
ds.Clear();
dataGridView1.DataSource = null;

cn.ConnectionString = connect.GetConnectionString();
string sel = "SELECT sid, sname FROM tblStudentInfo WHERE (sname LIKE '" + textBox2.Text + "%')";
MessageBox.Show(sel);
da = new SqlDataAdapter(sel, cn);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

Note:
SqlConnection cn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

No comments: