Wednesday, March 9, 2011

Search Data Using SqlDataReader

private void btnSearchSqlDataReader_Click(object sender, EventArgs e)
{
try
{
cn.ConnectionString = connect.GetConnectionString();
string sel_reader = "SELECT sid, sname FROM tblStudentInfo WHERE (sname LIKE '" + textBox2.Text + "%')";

cmd = new SqlCommand(sel_reader, cn);

cn.Open();
dr = cmd.ExecuteReader();

listBox1.Items.Clear();

if (dr.HasRows)
{
while (dr.Read())
{
listBox1.Items.Add(dr[1].ToString());
}
}
else
{
MessageBox.Show("No records found..");
}

dr.Close();
cn.Close();

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

Note:
SqlDataReader dr;

No comments: