简体   繁体   中英

StoredProcedure executes in SSMS, not in VB.NET application (no SqlException thrown)

I have a SQL StoredProcedure that performs two INSERT INTO operations in SSMS as expected. When executing this SP in my VB.NET application, it is executing (no SqlException thrown in Try block) but not executing the INSERT INTO commands. This application uses numerous SP's that all work without problems.

Code is as follows:

            Using (ParentMDI.dbCon)
                Dim sqlcmd As New SqlCommand("hyd_top_level_isr")
                With sqlcmd
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.AddWithValue("@part_num", part_num)
                    .Parameters.AddWithValue("@issue", issue)
                End With
                Try
                    sql.ExecuteNonQuery()
                Catch ex As SqlException
                    If DialogResult.Yes = MessageBox.Show("Error inserting top-level ISR." & vbCrLf & vbCrLf & "Send Error Report?", "Workflow Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) Then
                        ParentMDI.emailerrorstring = "Stored Procedure: hyd_top_level_isr"
                        ParentMDI.emailerrormessage = ex.Message
                        ParentMDI.ErrorEmail()
                    End If
                End Try
            End Using

For clarification;

I have inserted breakpoints before ExecuteNonQuery() . The sub does execute the ExecuteNonQuery() and the parameters being passed are populated with the correct values. I have also inserted a RETURN in the SP to return the SCOPE_IDENTITY() . This returns an empty string (not NULL , as I was expecting).

If any of you need more information, please let me know. I will be massively appreciative of anyone who can educate me on where I'm going wrong!

(This is my first time ever asking for help, please be kind!) :)

EDIT: Sorry guys. I seem to have lead you on incorrectly. The code pasted above is me trying all sorts of different attempts at trying to solve this. What I'll post now is what I should have posted in the first place. With the same outcome. Sorry for the confusion, and thanks for your attempts so far...

            Dim sqlcmd As New SqlCommand("hyd_top_level_isr", ParentMDI.dbCon)
            With sqlcmd
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.AddWithValue("@part_num", part_num)
                    .Parameters.AddWithValue("@issue", issue)
                End With
            Try
                sqlcmd.ExecuteNonQuery()
            Catch ex As SqlException
                If DialogResult.Yes = MessageBox.Show("Error inserting top-level ISR." & vbCrLf & vbCrLf & "Send Error Report?", "Workflow Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) Then
                    ParentMDI.emailerrorstring = "Stored Procedure: hyd_top_level_isr"
                    ParentMDI.emailerrormessage = ex.Message
                    ParentMDI.ErrorEmail()
                End If
            End Try

In addition to @Zohar Peled's comment I think you should set sqlCmd's connection and use sqlCmd.ExecuteNonQuery. Below you should also use local sqlConnection.

Using sqlcmd As New SqlCommand("hyd_top_level_isr", ParentMDI.dbCon)
    With sqlcmd
        .CommandType = CommandType.StoredProcedure
        .Parameters.AddWithValue("@part_num", part_num)
        .Parameters.AddWithValue("@issue", issue)
    End With
    Try
        sqlCmd.ExecuteNonQuery()
    Catch ex As SqlException
        '' Handle exception here
    End Try
End Using

Firstly I think that in SQLCommand you should pass the SQL connection like:

Dim sqlcmd As New SqlCommand("hyd_top_level_isr", ParentMDI.dbCon)

Secondly, you are not run sqlcmd but sql.executenonquery.

Third, executenonquery is an integer. If you dim a variable:

Dim result = sqlcmd.ExecuteNonQuery()

What do you get?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM