database library
-
Hi, I want to create a library for all the sql database operations.All other functions are working, but function for returning sqldatareader doesn't work.When I call this function from my page and try to access returned datareader object >it gives the error reader is closed. What to do???
-
Hi, I want to create a library for all the sql database operations.All other functions are working, but function for returning sqldatareader doesn't work.When I call this function from my page and try to access returned datareader object >it gives the error reader is closed. What to do???
No one can help you until you show us the code for returning sqldatareader. However, from the error code I suspect that the connection used by the reader is closed.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
No one can help you until you show us the code for returning sqldatareader. However, from the error code I suspect that the connection used by the reader is closed.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.
-
This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.
Your problem is that you closed the Reader in the Finally block. Whenever execution leaves the Try block FOR ANY REASON, the code in the Finally block is executed, where you close the Reader. Remove the code from the Try block and run it again. Your Try block is catching any and all errors and supressing them since you don't have a Catch block reporting the error.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.
As you you are using try finally block, the connection is closed so it is impossible to read from the reader as SqlDataReader requires open connection. So just return the reader without closing the connection.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
This is thecode in my class sqlDatareader ExecuteReader(string pStrQuery) { sqlDatareader dr; sqlCommand cmdObj=new sqlCommand(pStrQuery); try { cmdObj.open(); dr=cmdObj.ExecuteReader; return dr; } finally { cmdObj.close(); } } and this is how I use it: sqlDataReader dr=obj.ExecuteReader(query); and here it shows datareader is closed if try to read it.I even tried it without finally block.Not working.I'd appreciate any hep.
As the others have commented, you need to remove the close on the connection to return it. You should also ensure that the DataReader closes the connection for you. One method of doing this is to pass in
CommandBehavior.CloseConnection
as a parameter toExecuteReader
.Deja View - the feeling that you've seen this post before.
-
As the others have commented, you need to remove the close on the connection to return it. You should also ensure that the DataReader closes the connection for you. One method of doing this is to pass in
CommandBehavior.CloseConnection
as a parameter toExecuteReader
.Deja View - the feeling that you've seen this post before.
Hey Pete, Thanks for your post. Never realized that the CommandBehavior.CloseConnection enum existed. Very nice tip.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison