Why does this not work -- my error never shows up

E

Eagle

First I tried using the try, catch, throw, but it errored on the throw and I
don't want it to stop there, I want it to stop in the frm, not the class.
Next I set a Session to catch the error, and now I want to set the error
message label, but the error message label in teh form never appears. When
I stop the procedure and check to see if it's visible, it's true. It also
returns that the text says "Sql database doesn't exist" when sql is stopped.
So why does it not display the error?

form, page load:
cls.GetConnection
If Session("Error") <> "" then
lblError = cls.MessageLabel("ShowStopper")
Exit Sub
End If


Class:
Private Function GetConnection(ByVal connectionString As String) As
SqlConnection
Dim connection As New SqlConnection(connectionString)
connection.Open()
Return connection
End Function

Public Sub CheckConnection
Dim cnnSqlConn As SqlConnection = Nothing
Try
cnnSqlConn = GetConnection(mstrSQLConn)
Catch ext As Exception
'Throw New Exception("The connection with the database can´t be
established;" & ext.Message)
HTTPContext.Current.Session("Error") = ext.message
Finally
If Not cnnSqlConn Is Nothing Then
CType(cnnSqlConn, IDisposable).Dispose()
End If
End Try
End Sub


Public Function MessageLabel(strType as string) as label

Dim lbl as new label
Select Case strType
Case "ShowStopper"
lbl.Visible=True
lbl.Text = HTTPContext.Current.Session("Error")
lbl.ForeColor = Color.red
lbl.Font.Bold = true
End Select
Return lbl
End Function
 
K

Kevin Spencer

Of course it "errors" on the Throw, because you're throwing an exception.
And since you're throwing the exception without handling it, execution
stops, which is what always happens with an unhandled exception.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
E

Eagle

Okay, that was my first question. However, I have commented out the throw
and created a session variable to catch the error on the form, then display
the error on a label. The label remains invisible for unknown reasons.
That is my second question, and actually more important because I am not
using the throw anymore. Sorry I wasn't clear.
 
K

Kevin Spencer

No problem. It's just that the code you posted would have stopped right
there. How about posting the revised code?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top