1

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
1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.