2014年7月13日 星期日

[RESOLVED] Error Display on Form


Hi, I have this code that works on my form with a database table insert:


Try

con.Open()

cmd.ExecuteNonQuery()

LabelMsg.Text = "Record Inserted Successfully"


Catch ex As Exception

Throw ex

Finally

con.Close()

con.Dispose()

End Try

End Sub

If I insert a row that violates an index I setup, I throw an exception on the "Finally" line indicating Cannot insert duplicate key row in object 'dbo.link' using unique index...and so on. This works as expected, but I would like to instead display the error
in a label on my web form. How do I do this? Thanks.



        Try

con.Open()

cmd.ExecuteNonQuery()

LabelMsg.Text = "Record Inserted Successfully"


Catch ex As Exception

Label1.Text = "Error happening : " & ex.Message

Finally

con.Close()

con.Dispose()

End Try



I prefer to use SQLException, with something like:


Try
SqlDataSource1.Insert()
Catch ex As SqlException
Dim errNo As Integer = ex.Errors(0).Number
lblMsg.ForeColor = Drawing.Color.Red
If errNo <> 2627 And errNo <> 2601 Then
lblMsg.Text = "Error:
" & ex.Message
Else
lblMsg.Text = "Error:
There is alread an entry with UserId of " & UserId.ToString & _
"
DATA WAS NOT ADDED"
End If
End Try

To list all SQL Error Messages in english:


SELECT * FROM sysmessages
WHERE msglangid = 1033




 


 


沒有留言:

張貼留言