• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 104
  • Last Modified:

Checking for Errors on a Stored Procedure from within a VB.Net app?

I want to check for errors from within my VB.Net app when I run a Stored Procedure. For example, if a database table was changed and that change wasn't reflected in a SP, it will throw an exception. How can I check for that in my VB.Net?

Thanks!
0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 3
  • 2
1 Solution
 
sammySeltzerCommented:
Hi,

Use Try/Catch expression to do so.

For instance,

Try
Your operations here (insert or delete or update or select

        Catch ex As Exception

            'If error, notify the user

            Label1.Text = ex.Message

        End Try
        Conn.Close()

The part you are looking for is the ex.Message

Whatever message your stored proc displays when an error occurs, is transmitted using ex.Message.

To get that to work though, have a label on your markup.

In my case, label is called label1
0
 
BlakeMcKennaAuthor Commented:
I actually do that right now. The reason I asked is because I performed a INSERT transaction and the action timed out. That's the first time that's ever happened. The error message returned in the "Catch" portion was that the action "timed out"...which is very vague. I need more information in order to debug the issue. I eventually figured it out that it was the Stored Procedure. But I want my VB.Net to catch any errors that are thrown in the SP.
0
 
sammySeltzerCommented:
Ok, I have a better understanding of your question now.

You are right, in situations such as that, try/catch is usually vague.

I usually do two things,

1, add debug="true" at top of the markup page.
2, I comment out the try/catch.

In the absence of these two instances, I am not sure vb.net is capable of catching errors not caused by your SP.

And if it is your SP causing an error, it needs to be resolved on the backend first.

I am pretty sure you know that anyway.

You can log at log files and even viewer for any issues that could be causing the timeout issue.

Code optimization could be looked at as well.

Good luck.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BlakeMcKennaAuthor Commented:
This is a windows app!
0
 
Jacques Bourgeois (James Burger)Commented:
Do not simply catch Exception. Catch exceptions for our data library, such as SqlException or OleDBException. They offer much more informations and the generic Exception. Note that this is not only for databases, this is for almost any type of exception.

A SqlException object for instance has a lot of properties that can be useful, either in debugging or at runtime when you want to decide what action to take or what message to display in case of an error. It gives you a Number property that us useful if you need to consult the SQL Server documentation for more information about what the error message means. It has a Procedure property that tells you what procedure triggered the error. And, hold your hat properly, it also has a LineNumber property that tells you on which line of the stored procedure the error occurred.

At another level, read the documentation carefully about the return values of the different methods that you use for database access. For instance, the ExecuteNonQuery method of the Command object returns the number of rows that were affected. This can be very useful for diagnosing problems that do not trigger execptions. If it returns 0, no change was made in the database. This will not trigger an error, so you won't be able to get it with a Try...Catch. And the information you receive is not clear. But you know something happens. It can be expected, in some situations that an SQL insert, update or delete does not give results. But other times, a returned value of 0 points to the fact that your parameters are not passed correctly, or that the SQL does no to what you thought it would do, or that you never though that a user could misspell Vnited States.
0
 
BlakeMcKennaAuthor Commented:
Thanks James...I didn't realize all that was possible to trap with the Catch phrase.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now