yangbin991
asked on
vb.net how to do IF err.number = 2627 Then errMsg = "This student ID has exists! "
Hi, every one.
Nice see you again.
I stuck at the exception handling part of my project. I feel VB.net has a lousy exception handling.
In vb6, i can catch exception , check the err.number and then use my own text to alert user.
For example:
if it is connection failed, then use my own error text like "Hi, brother turn on that blue PC. SQL server is inside."
If it is violation of primary key, then I can use "Dont register a tag with a existing ID. Please use a new ID."
Now in vb.net, they say i should catch different specific exceptions and put my own error message.
There are two problem i met, first I cannot find what is that specific exception i wanna catch.
For example, I wanna catch the following error
Message "Error: 2627: Violation of PRIMARY KEY constraint 'PK__TBL_TAG_INFO__505BE5A D'. Cannot insert duplicate key in object 'TBL_TAG_INFO'.
The statement has been terminated."
Who can tell me the specific exception?
Another Problem, i cannot decide which is narrow excpetion?
See my following code.
I put catch sqlexception before generic exception. Good, inside sqlexception , i can use err.num.
However some error like primary key Violation cannot be caught by sqlexception. It is caught by generic exception. i dont know the reason. maybe primary key Violation is not under sqlexception.
Then In generic exception, there is no err.number. What should i do?
I plan to trim the error number from the Ex.Message. But it seems quite funny. Shouldsome better method be there, right?
'************************* ********** ********** **********
try
dim objDatabase as new clsDatabase("connectionStr ")
iNoAffected = objDatabase.ExecSPNoReturn (strProcName, arrParams)
if iNoAffected = 1 then
RowTrackData(myRow,CType(M e.iTagID,I nt32),sTag Type, "Success","")
End If
catch sqlEx as SqlException
dim errMsg as String
if sqlEx.Number = 17
' Server: Msg 17, Level 16, State 1
' SQL Server does not exist or access denied
errMsg = "SQL Server does not exist or access denied"
elseif sqlEx.Number = 17142
' Server: Msg 17142, Level 16, State 1
' SQL Server has been paused. No new connection will be allowed
errMsg = "SQL Server has been paused. No new connection will be allowed"
elseif sqlEx.Number = 18456
' Server: Msg 18456, Level 16, State 1
' Login failed for user 'sa'
errMsg = " Login to database failed"
end if
RowTrackData(myRow, _
CType(Me.iTagID,Int32), _
sTagType, _
"<font color='red'>Failed</font>" , _
sqlEx.Message)
catch ex as Exception
dim errNum as Int64
errNum = System.Runtime.InteropServ ices.Marsh al.GetHRFo rException (ex)
if errNum= -2146233088 then
else if errNum= -2146233088 then
End If
RowTrackData(myRow, _
CType(Me.iTagID,Int32), _
sTagType, _
"<font color='red'>Failed</font>" , _
"This tag has been existing. Cannot register again.")
finally
' Create a DataView using the DataTable.
'myDataView = New DataView(myDataTable)
end try
'************************* ********** ********** ********** *
Nice see you again.
I stuck at the exception handling part of my project. I feel VB.net has a lousy exception handling.
In vb6, i can catch exception , check the err.number and then use my own text to alert user.
For example:
if it is connection failed, then use my own error text like "Hi, brother turn on that blue PC. SQL server is inside."
If it is violation of primary key, then I can use "Dont register a tag with a existing ID. Please use a new ID."
Now in vb.net, they say i should catch different specific exceptions and put my own error message.
There are two problem i met, first I cannot find what is that specific exception i wanna catch.
For example, I wanna catch the following error
Message "Error: 2627: Violation of PRIMARY KEY constraint 'PK__TBL_TAG_INFO__505BE5A
The statement has been terminated."
Who can tell me the specific exception?
Another Problem, i cannot decide which is narrow excpetion?
See my following code.
I put catch sqlexception before generic exception. Good, inside sqlexception , i can use err.num.
However some error like primary key Violation cannot be caught by sqlexception. It is caught by generic exception. i dont know the reason. maybe primary key Violation is not under sqlexception.
Then In generic exception, there is no err.number. What should i do?
I plan to trim the error number from the Ex.Message. But it seems quite funny. Shouldsome better method be there, right?
'*************************
try
dim objDatabase as new clsDatabase("connectionStr
iNoAffected = objDatabase.ExecSPNoReturn
if iNoAffected = 1 then
RowTrackData(myRow,CType(M
End If
catch sqlEx as SqlException
dim errMsg as String
if sqlEx.Number = 17
' Server: Msg 17, Level 16, State 1
' SQL Server does not exist or access denied
errMsg = "SQL Server does not exist or access denied"
elseif sqlEx.Number = 17142
' Server: Msg 17142, Level 16, State 1
' SQL Server has been paused. No new connection will be allowed
errMsg = "SQL Server has been paused. No new connection will be allowed"
elseif sqlEx.Number = 18456
' Server: Msg 18456, Level 16, State 1
' Login failed for user 'sa'
errMsg = " Login to database failed"
end if
RowTrackData(myRow, _
CType(Me.iTagID,Int32), _
sTagType, _
"<font color='red'>Failed</font>"
sqlEx.Message)
catch ex as Exception
dim errNum as Int64
errNum = System.Runtime.InteropServ
if errNum= -2146233088 then
else if errNum= -2146233088 then
End If
RowTrackData(myRow, _
CType(Me.iTagID,Int32), _
sTagType, _
"<font color='red'>Failed</font>"
"This tag has been existing. Cannot register again.")
finally
' Create a DataView using the DataTable.
'myDataView = New DataView(myDataTable)
end try
'*************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.