Solved

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

Posted on 2016-09-15
4
56 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 28

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 250 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 50

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

831 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