Click here to Skip to main content
15,888,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hello everyone,

I am executing a Sql Server 2008 query which returns a set of rows. When i execute the same query from code, using SqlDataReader ,reader.Read() returns null. Any ideas??

The query is a simple Select Statement,with two declared variables which are used in Where Clause.

Thnx in advance
Posted
Updated 20-Jul-20 11:47am
Comments
Richard C Bishop 4-Sep-13 10:59am    
You need to show your code, we cannot read minds or access your hard drive.
[no name] 4-Sep-13 10:59am    
Any ideas? Sure, since the query returns data in SSMS and returns nothing in your code, then you have done something wrong. Any more than that we cannot tell since we cannot remote view your code or read your mind.
IviKAZAZI 4-Sep-13 11:05am    
I solved it,the code was ok,i just addad a line of code cmd.ExecuteNonQuery(); before this line rdr = cmd.ExecuteReader(); and it worked. I dont know why this happend but its ok now. Hope this helps anyone else...

Reader.Read does not return null - it can't, the return type is bool which is a value type, and as a result is not nullable.

Probably, what you mean is that it returns no rows, or reader is null and the attempt to use it throws a "null reference" exception.

So, place a breakpoint on the first line of the method, and single step through your code looking at the variables, and what is going on. If you can't work it out fro yourself from that, we would need to see the code that generated the problem, and a better description of exactly what the problem is.
 
Share this answer
 
Comments
IviKAZAZI 9-Sep-13 6:01am    
U are right about the NULL, it returned no rows. But i resolved it just by executing the command with ExecuteNonQuery before i executeReader and it worked. Maby the problem is with some decalred sql variables on top of the query.
I solved it,the code was ok,i just addad a line of code

C#
cmd.ExecuteNonQuery(); 


before this line

C#
rdr = cmd.ExecuteReader(); 


and it worked. I dont know why this happend but its ok now. Hope this helps anyone else...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900