the following is the code,...When i run this code I get "Invalid operation exception", Trying to read when no data is present, but the query returns result in query analyser...can anyone suggest what the problem is ...
Connection is open and the query is simple select statement
SqlCommand myCommand =newSqlCommand(query,myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
Thanks
Niranch
niranch,
can you show me exact syntax of your query string within the context of how you are using it in the c# code? You may have some quotes being escaped incorrectly or something such that when run from the app code, it does not return any results and therefore you are getting your error. so, make sure that your query string is written correctly, particularly how you have used single or double quotes...--jp
|||hi jp,
thank you for your suggestion...I just figured out the problem...
It is just that, when I run the same set of statements in my code behind file, I get the result, but when I run it in a seperated .CS file(DAL), I get the error mentioned in my post...I am trying to return datareader object to the Business tier...
I dont know why...
Thanks
Niranch
|||ahh so you are having trouble with the data moving through tiers. can you show some code of how your are interacting between the tiers?|||hi this is my code...the same set to commands ,connections works fine in codebehind file
codebehid file
myReader = obj.getdbvalues("Server", sName.ToString()); (obj is the instance of the class - the class where i have access to Database)
class where i have database access
connectionString = BuildConnectionString().ToString();
string query = "select * from users";
myConnection.ConnectionString = connectionString;
SqlCommand myCommand =newSqlCommand(query,myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
return(myReader);
|||connectionString = BuildConnectionString().ToString();
string query = "select * from users";
myConnection.ConnectionString = connectionString;
SqlCommand myCommand =newSqlCommand(query,myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
return(myReader); <-- at this point is your reader null? if you put a throw statement here like if(myReader == null) throw new Exception("Null Reader") will you get an exception? is the reader already null here, or are you getting results here and just not when you set
myReader = obj.getdbvalues("Server", sName.ToString()); (obj is the instance of the class - the class where i have access to Database) you get the null reader here? that make sense? at what point are you not getting any results? -- jp
|||I get the problem, Exactly after executing the command
DAL Page
myreader = mycommand.executereader(), i get a null reader. "Invalid read attempt when there is no data" is the error...only in this page, i have my connection open...
i dont know if it is ok to send a datareader object as return object to another page............
|||thankyou, i got the problem solved,,,
I transfered the contents from reader to datatable and passed as return objec to presentation tier...It worked fine.............