Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET with sql 2k

Posted on 2006-11-24
5
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

618 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