Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / April 2004

Tip: Looking for answers? Try searching our database.

Passing a DataReader between methods and getting RETURN_VALUE

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Raychev - 20 Apr 2004 17:49 GMT
Hi all,

I have the following problem:

I have a private method that returns a SqlDataReader. For this to work I
have not to close the DB connection in the above method. I do this only to
have the possibility to iterate through the entire rows set in a while loop,
located in the calling method.

I have included a few lines of code to get the number of rows fetched from
the DB. I do this with SqlParameter("RETURN_VALUE", SqlDbType.Int) [ I am
using a stored procedure that returns @@rowcount].

I have found out that I am getting the appropriate value for the stored
procedure parameter ("RETURN_VALUE") ONLY when I have explicitly close the
DB connection. Unfortunately when the control is returned to the calling
method the usual error message:

Invalid attempt to Read when reader is closed

is being received as the connection is closed and there's no such DataReader
already.

Does anyone know a workaround for this? I need the report to the user the
number of fetched rows.

Thanks,

Martin

--------------------------

Code:

private SqlDataReader GetReader(string parameter, string date)

{

     DateTime MyDateTime;

.....//. date parsing

     SqlConnection myConn = new SqlConnection(ConnectionString());

     SqlCommand myCmd = new SqlCommand();

     SqlDataReader myReader=null;

     myCmd.CommandType = CommandType.StoredProcedure;

     myCmd.Connection = myConn;

     myCmd.CommandText = "GetData";

     myCmd.CommandTimeout = 250;

     SqlParameter Param1 = new SqlParameter();

     Param1 = myCmd.Parameters.Add("@parameter", SqlDbType.VarChar, 12);

     Param1.Direction = ParameterDirection.Input;

     Param1.Value = parameter;

     SqlParameter Param2 = new SqlParameter();

     Param2 = myCmd.Parameters.Add("@date", SqlDbType.VarChar, 20);

     Param2.Direction = ParameterDirection.Input;

     Param2.Value =  MyDateTime.Date.ToShortDateString();

     SqlParameter outValue = new SqlParameter();

     outValue = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);

outValue.Direction = ParameterDirection.ReturnValue;

     myConn.Open();

     myReader = myCmd.ExecuteReader();

// this works only is myConn.Close() is executed but then

// we cannot return a READER to the calling method???

intRowsReturned = Convert.ToInt32(myCmd.Parameters["RETURN_VALUE"].Value);

                 return myReader;

           }

// the calling method

private void btnSQLGet_Click(object sender, System.EventArgs e)

{

                 .

     // get the data in a reader

     SqlDataReader myReader = GetReader(cboParameter.Text, txtDate.Text);

     if (myReader==null)

           return;

.

}
Alvin Bruney [MVP] - 20 Apr 2004 18:44 GMT
a datareader is a forward only cursor, either keep it open for the duration
and then close or use some other structure

Signature

Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok

> Hi all,
>
[quoted text clipped - 109 lines]
>
> }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.