Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Opening an ADO Recordset from a SQL Server Stored Procedure using VB.NET

Posted on 2016-09-15
4
Medium Priority
?
94 Views
Last Modified: 2016-09-16
The posted code in VB.net doesn't display a MsgBox.  Typing "EXEC procUserOperations 154" from a query windows in SQL Server works.  The VB.net program doesn't output an error or "bomb out".  It just continues on with its execution.  I can even purposefully type a stored procedure name that doesn't exist for the CommandText and the program still doesn't "bomb out" or give an error message.  What's wrong with this code, and how can I debug this program?  The stored procedure returns a recordset and its declaration is :

ALTER PROCEDURE [dbo].[procUserOperations]
      @ParentOperationId As Int
As
BEGIN

       Dim cn As New ADODB.Connection
        Dim cmd As New ADODB.Command
        Dim Param As New ADODB.Parameter
        Dim rs As New ADODB.Recordset

        cn.ConnectionString = strConn2
        cn.Open()
        With cmd
            .CommandText = "procUserOperations"
            .CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
            .ActiveConnection = cn
            Param = .CreateParameter("ParentOperationId", ADODB.DataTypeEnum.adInteger,
                                     ADODB.ParameterDirectionEnum.adParamInput,, 154)
            .Parameters.Append(Param)
            rs = .Execute
        End With
        MsgBox(rs.Fields("UserId").Value & "1")

Open in new window

0
Comment
Question by:Declan_Basile
4 Comments
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41800850
Try this ..

MessageBox.Show( "The username or password you entered is incorrect", _
        "Login Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop)
0
 
LVL 10

Accepted Solution

by:
Duy Pham earned 1000 total points
ID: 41800868
I don't see any error with your code. Just some suggestions:
1. Make sure that your code is running within an Windows Application, otherwise MsgBox won't work
2. Check if the caller of your function catches and swallows the exception. Or wrap your code into a Try Catch then show the message as below
        Dim cn As New ADODB.Connection
        Dim cmd As New ADODB.Command
        Dim Param As New ADODB.Parameter
        Dim rs As New ADODB.Recordset

        Try
                cn.ConnectionString = strConn2
                cn.Open()
                With cmd
                    .CommandText = "procUserOperations"
                    .CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
                    .ActiveConnection = cn
                    Param = .CreateParameter("ParentOperationId", ADODB.DataTypeEnum.adInteger,
                                     ADODB.ParameterDirectionEnum.adParamInput,, 154)
                    .Parameters.Append(Param)
                    rs = .Execute
                End With
                MsgBox(rs.Fields("UserId").Value & "1")
        Catch ex As Exception
                MsgBox(ex.Message)
        End Try

Open in new window

0
 
LVL 54

Assisted Solution

by:Ryan Chong
Ryan Chong earned 1000 total points
ID: 41800900
>>I can even purposefully type a stored procedure name that doesn't exist for the CommandText and the program still doesn't "bomb out" or give an error message.

as a start for debugging, try make some break points in your codes and debug it with Watches. make sure your program runs through the codes where you put in the break points.
0
 
LVL 1

Author Closing Comment

by:Declan_Basile
ID: 41801962
Thanks.  I ended up converting the code to use SQLDataReader.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

971 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