Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
| Search | Today's Posts | Mark Forums Read
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 1st, 2005, 01:37 PM
Authorized User
Join Date: Jun 2003
Location: Irvine, CA, USA.
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default Store Results From DataReader


I want to create a Function that returns the result of a parameter Stored Procedure in a DataTable if that's possible. I've tried a couple of things with no success. The reason I need to do this is I have to close the SQL connection before exiting the Function. If I do this, my returned recordset disappears.

Here's my code to get the recordset using SQLCommand:

Public Function GetRecordSet() as New DataTable
Dim sConnString As String = "Data Source=" & SqlServerName & "; User ID=" & SqlLogin & "; Password=" & SqlPWD & "; Initial Catalog=" & mcWebPPO
Dim oConn As New SqlConnection(sConnString)
Dim oComm As New SqlCommand("MySP", oConn)
oComm.CommandType = CommandType.StoredProcedure

'*** Allow for SP's Return Value
Dim oParm As SqlParameter = oComm.Parameters.Add(mcReturnValue, SqlDbType.Int)
oParm.Direction = ParameterDirection.ReturnValue

'*** Setup the input parameter
oParm = oComm.Parameters.Add("@SubjectSearch", SqlDbType.VarChar, 50)
oParm.Value = "What my parameter is"

Dim oDR As SqlDataReader = oComm.ExecuteReader

Do While oDR.Read()
   Console.WriteLine("{0}, {1}", oDR.GetName(0), oDR.GetName(1))
   Console.WriteLine("{0}, {1}", oDR.GetValue(0), oDR.GetValue(1))

GetRecordSet = ?????
Return GetRecordSet


Console.WriteLine(mcReturnValue & ": {0}", oComm.Parameters(mcReturnValue).Value)

End Function


  #2 (permalink)  
Old February 2nd, 2005, 12:59 AM
Friend of Wrox
Join Date: Oct 2003
Location: Chennai, TamilNadu, India.
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi

Set the return Parameter Value to variable and return that variable.It wont disappear.If you want to set those values in DataTable .Create an dataset and add those two return values in the DataTable and return the dataset.
  #3 (permalink)  
Old February 2nd, 2005, 11:18 AM
Authorized User
Join Date: Jun 2003
Location: Irvine, CA, USA.
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks Santhi for the suggestion, I never thought of using a Variable.
I figured out a way to use the SQLCommand and SQLDataAdapter to run a Stored Procedure that returns the results in a DataTable. That works for me.


Similar Threads
Thread Thread Starter Forum Replies Last Post
datareader MunishBhatia ASP.NET 2.0 Professional 2 October 17th, 2007 07:05 AM
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM
Datareader NitinJoshi ADO.NET 4 January 31st, 2005 08:34 AM
DataReader cjcd BOOK: Beginning ASP.NET 1.0 2 March 21st, 2004 01:06 PM
How to store results of several scripts in table fawadr4656abp Oracle 0 June 13th, 2003 03:00 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.