Solved

ASP.NET with sql 2k

Posted on 2006-11-24
5
209 Views
Last Modified: 2010-04-06
I have a sql server stored procedure which returns a resultset and also has out params. I want to execute this SP from .Net. But I have a choice of using ExecuteReader method or ExecuteNonQuery method. If I execute with ExecuteReader method I get resultset only. If I execute with ExecuteNonQuery method, I get out param values.
 
Does anyone know how to execute SP only once and get resultset and out param values?

Your help as before is much appreciated.

Regards
Wayne
0
Comment
Question by:Wayne29
  • 3
  • 2
5 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 18006846
Hi Wayne29,

You can get the output parameters, however you can only do so after you have closed the datareader!

Tim Cottee
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 18006855
Wayne29,

If you really need them first, consider making the stored procedure return mutiple resultsets, using

Select @OutputParam1,@OutputParam2

Select ...... your existing statement

You can then use read the datareader for the first resultset to get the "output" parameters and then use datareader.nextresult() to advance the datareader to the second resultset for the other records.
       

Tim
0
 

Author Comment

by:Wayne29
ID: 18006859

Hi Tom,

Thanks for reply.

My Question is, I should be able to get RESULTSET (interms of reader / dataset anything is OK) along with OUT values from SP.

If possible, can i have pseudo code / code snippet.

Thanks
Wayne
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 18006869
Wayne29,

        Dim cn As SqlClient.SqlConnection = New SqlClient.SqlConnection("A Connection String")
        cn.Open()
        Dim sc As SqlClient.SqlCommand = New SqlClient.SqlCommand
        Dim dr As SqlClient.SqlDataReader
        Dim result As String
        With sc
            .Connection = cn
            .CommandText = "Test"
            .CommandType = CommandType.StoredProcedure
            .Parameters.Add("@Value", SqlDbType.Int).Direction = ParameterDirection.Output
            dr = .ExecuteReader()
            MsgBox(.Parameters("@Value").Value)
            With dr
                While .Read
                    result &= .Item(1)
                End While
                MsgBox(sc.Parameters("@Value").Value)
                .Close()
                MsgBox(sc.Parameters("@Value").Value)
            End With
        End With
        MsgBox(result)

This snippet shows when the output parameter gets returned, you can see when you run it (replacing the connectionstring, sp name, parameter etc for your situation). That the msgbox comes up empty until the dataread has been closed.

As this was a quick snippet in vb.net, you may need to replace the msgbox calls or debug it to see this.

Tim
0
 

Author Comment

by:Wayne29
ID: 18006874
Thanks Tim. You are correct.
Wayne
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now