Nuke
Nuke

Reputation: 1259

Show SQL error in ASP.net

I have the following stored procedure. But I don't know how to throw an error from SQL Server and show it on JavaScript Alert.

Here is the stored procedure

Begin
    if EXISTS(select * from POS_Transactions where ID = @transId) 
    Begin
        declare @totalAmount money

        select @totalAmount = TotalAmount 
        from POS_Transactions 
        where ID = @transId and PAN = @pan

        update pos_transactions 
        set transactiontypeid = 3 
        where id = @transId

        update cardbalance 
        set cardbalance = (cardbalance + @totalAmount),
            totalredemption = (totalredemption - @totalAmount) 
        where pan = @pan
    END
    Else
        --Show Error Here
    BEGIN
END

And here is the VB code:

Try
  If txtEmbossLine.Text <> "" And txtTransactionId.Text <> "" Then
  Utilities.VoidRedemption(txtEmbossLine.Text, txtTransactionId.Text)
  Response.Write("<script>alert('Redemption Voided successfully')</script>")
  Else
  Response.Write("<script>alert('Please enter both fields')</script>")
  End If
Catch ex As Exception
  Response.Write(ex.Message)
End Try

Upvotes: 1

Views: 1136

Answers (2)

Musakkhir Sayyed
Musakkhir Sayyed

Reputation: 7170

Catch SQL exception in vb.net.

Try
     //Do your stuff
Catch ex as SqlException
  Dim errors = ex.Errors
  // inspect errors to decide if this is the condition you want to catch
  Dim shouldCatch As Boolean = ... 
     // Code that acts on the specific error condition
  If shouldCatch Then
  Else
     Throw // rethrow error
  End If
 End Try

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157136

You can use this code to throw an exception from SQL Server:

throw 50001, 'Some error', 1

Your try ... catch in your C# code will do the rest for you.

Upvotes: 1

Related Questions