Solved

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

Posted on 2014-12-12
6
94 Views
Last Modified: 2014-12-15
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
Comment
Question by:BlakeMcKenna
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
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
 

Author Comment

by:BlakeMcKenna
Comment Utility
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
 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:BlakeMcKenna
Comment Utility
This is a windows app!
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:BlakeMcKenna
Comment Utility
Thanks James...I didn't realize all that was possible to trap with the Catch phrase.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now