Reputation: 712
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?
Upvotes: 2
Views: 1377
Reputation: 171
You could load your data from a SQLDataAdapter in to a DataSet and use the following:
if (DataSet.Tables[0].Rows.Count <=0)
{
}
Upvotes: 0
Reputation: 6130
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
Upvotes: 4
Reputation: 44605
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.
Upvotes: 1