I am having one table say emp, in that i dont have any values. If i using this query "select * from emp" in the asp.net coding like below:
con.Open();
String str="select * from emp where empname='Abdul'";
cmd=new SqlCommand(str,con);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
textBox1.text=dr[0].ToString();
textBox2.text=dr[0].ToString();
textBox3.text=dr[0].ToString();
}
con.Close();
In emp tables, i dont have any empname as abdul, When i am doing this it should show some errors, how to do it?
Do something like this:
if (dr == null || !dr.HasRows)
{
//NO record found
lblError.Text ="No Records Found!";
}
else
{
//Record Found SUCCESS!
while(dr.Read())
{
textBox1.text=dr[0].ToString();
textBox2.text=dr[0].ToString();
textBox3.text=dr[0].ToString();
}
}
Regards
No it does not show any error because you enter the while only if there are some results in the query.
I would use an if/else and if DataReader has no content because no results found, I would notify the user or show something in the UI, depends on the type of application and on your exact needs.
You could load your data from a SQLDataAdapter in to a DataSet and use the following:
if (DataSet.Tables[0].Rows.Count <=0)
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.