sqlcommad cmd=n ew sqlcommad("select sum(toffee) from table where name='ravi'");
sqldatareader dr=cmd.executereader();
if(dr.read)
{
label.text=dr.getint(0);
}
else
{
label.text="0"
}
problem cms here even there is no name in table with ravi still dr reads when i debug my programm and show me null excepton as it returns null value form table.why it is not going in else part
sqlcommad cmd=n ew sqlcommad("select sum(toffee) from table where name='ravi'");
[snip]
problem cms here even there is no name in table with ravi still dr reads when i debug my programm and show me null excepton as it returns null value form table.why it is not going in else part
someone pls help me??????????
Simply, because there's no "ravi" record - at least among the shown ones :-) and you might not have it at all in your table :-)
One advice, it works for me and it may work for you as well :-)
When I am doing SQL statements, no matter the technology of destination, I always try them in the default query environment first, be it SQLServer Management Studio or something else. When I'm sure it works, I move it in code.
Cheers,
Andrew
Mr. Andrea Raimondi
Senior Software Analyst/Developer
rajeevinime
Member
406 Points
229 Posts
data reader problem
Sep 19, 2010 05:40 AM|LINK
i have a sql table like
name toffees
rajeev 12
raj 14
rajeev 16
now the following code i have in aspx.cs
sqlcommad cmd=n ew sqlcommad("select sum(toffee) from table where name='ravi'");
sqldatareader dr=cmd.executereader();
if(dr.read)
{
label.text=dr.getint(0);
}
else
{
label.text="0"
}
problem cms here even there is no name in table with ravi still dr reads when i debug my programm and show me null excepton as it returns null value form table.why it is not going in else part
someone pls help me??????????
Rajeev Kumar
Mark As Answer If It Helps You
reza141414
Participant
1571 Points
497 Posts
Re: data reader problem
Sep 19, 2010 07:49 AM|LINK
hi...
use: dr.HasRow to find out whether u have any row or not....
............................
dont forget to mark as answerd...
ketan_al
Contributor
6850 Points
1143 Posts
Re: data reader problem
Sep 19, 2010 08:10 AM|LINK
Hi,
You can use SqlDataReader.IsDBNull, please refer following
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.isdbnull%28v=VS.90%29.aspx
hope this helps
MCP, MCTS ( ASP.NET 3.5 )
Please mark as answer if it helps :)
reza141414
Participant
1571 Points
497 Posts
Re: data reader problem
Sep 19, 2010 08:11 AM|LINK
hi...
also i suggest u to use LIKE sql statment for comparing strings...
go here to learn more:
http://www.w3schools.com/SQL/sql_like.asp
.......................................
dont forget to mark as answerd...
dont forget to mark as answerd...
sansan
All-Star
53942 Points
8147 Posts
Re: data reader problem
Sep 19, 2010 09:26 AM|LINK
Firstly, you do not need to use a datareader when you are retrieving just one value. Use ExecuteScalar instead
And you can use ISNULL function to get 0 if no records are found.
DeerBear
Member
47 Points
25 Posts
Re: data reader problem
Sep 19, 2010 09:58 AM|LINK
Simply, because there's no "ravi" record - at least among the shown ones :-) and you might not have it at all in your table :-)
One advice, it works for me and it may work for you as well :-)
When I am doing SQL statements, no matter the technology of destination, I always try them in the default query environment first, be it SQLServer Management Studio or something else. When I'm sure it works, I move it in code.
Cheers,
Andrew
Senior Software Analyst/Developer