Solved

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

Posted on 2014-12-12
6
96 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
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 28

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Move a class from Solution Items to a project 2 33
Help with exporting to excel 4 36
Help with consolidating excel files using VB.net 2 29
Open a link in vb.net 2 16
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

837 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