Solved

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

Posted on 2014-12-12
6
99 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40496444
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
ID: 40496567
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 29

Expert Comment

by:sammySeltzer
ID: 40496629
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!

 

Author Comment

by:BlakeMcKenna
ID: 40496915
This is a windows app!
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40498274
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
ID: 40500422
Thanks James...I didn't realize all that was possible to trap with the Catch phrase.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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