VB.NET 2005 - exception question

G

Guest

I am working on a application that uses the MyApplication_UnhandledException
event to basically tell the user that I screwed up somewhere...see below

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e
As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
Handles Me.UnhandledException
MsgBox(e.Exception.Message.ToString)
e.ExitApplication = False
End Sub

My test app that I was working on has 3 controls on it, A menu bar, textbox,
and datagrid. The app will take what I have in the textbox aka SQL Query,
then use the dataadapter to fill a dataset then bind it to the datagrid. Here
is where it gets weird, or at least I have no idea why this happens... First
here is my code...

I have a SQL class, which takes the query, and connection string and returns
a datatable

Imports System.Data
Imports System.Data.SqlClient

Public Class SQL

Private pResults As New DataTable
Private pQuery As String
Private pConnStr As String

Public ReadOnly Property Results() As DataTable
Get
Return pResults
End Get
End Property

Public Sub New(ByVal ConnStr As String, ByVal Query As String)
pConnStr = ConnStr
pQuery = Query
End Sub

Public Function ExecuteQuery() As DataTable
Dim DBConn As SqlConnection
Try
DBConn = New SqlConnection(pConnStr)
DBConn.Open()

Dim adp As New SqlDataAdapter(pQuery, DBConn)
adp.Fill(pResults)

Return pResults
Catch ex As Exception
Throw ex
Finally
DBConn.Close()
End Try
End Function
End Class

Also here is my main body of the testForm, the menubar has two items one
Parent (File) then a child node of Run with a shortcut key set to Ctrl+R

Private Const CONNECTION_STRING As String =
"uid=sa;password=SomePassword;initial catalog=SomeDatabase;data
source=wwwsql01,1433;connect timeout=15;persist security info=false;network
library=dbmssocn"

Private Sub RunToolStripMenuItem_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles RunToolStripMenuItem.Click
RunQuery()
End Sub

Private Sub RunQuery()
Dim qry As New SQL(Me.CONNECTION_STRING, TextBox1.Text)
qry.ExecuteQuery()

DataGridView1.DataSource = qry.Results
End Sub

Very simple... If I run it as is and an exception is thrown when I press
Ctrl+R while in the textbox, the unhandled exception handler will take over
and show me the exception but then place an "r" into the textbox. If I hit
Ctrl+R from the datagrid all is fine. If I use the mouse to click on Run all
is fine. So If I place a Try Catch around where I call RunQuery() and do not
use the MyApplication_UnhandledException but show a messagebox in the Try
Catch instead...The "r" as mentioned above does not show up at all. Would
anyone know the reasoning behind this? This app is nothing special just a
small test before I develop the larger version. All it does is run a SQL
query... Thanks in advance for anyhelp that you may be able to offer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top