Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a stored procedure to insert data into two tables in sql 2008 server. Now while handling exception I am using exception code to check the exception and accordingly i am showing the error message to the user.Can u tell me a better option to do the same.

VB
Public Sub rtrnQry(ByVal usrNm As String, ByVal psWd As String, ByVal fNm As String, _
ByVal lNm As String, ByVal empId As Integer, ByVal proNm As String, ByVal aDm As String, ByVal rowno As Integer)
Try

Using cn As New SqlConnection(sqlConnStr)
cn.Open()
Using cmd As New SqlCommand("InsrtLogin", cn)
With cmd
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue("@empid", empId)
.Parameters.AddWithValue("@username", usrNm)
.Parameters.AddWithValue("@password", psWd)
.Parameters.AddWithValue("@firstname", fNm)
.Parameters.AddWithValue("@lastname", lNm)
.Parameters.AddWithValue("@adminpri", aDm)
.Parameters.AddWithValue("@proName", proNm)
.ExecuteNonQuery()
End With
End Using
End Using

Catch ex As SqlException
If chkBox1_Crtusr.Checked Then
If ex.Number.Equals(2627) Then 'Exception code for Primary Key Violation
MsgBox("Duplicate Employee ID cannot be inserted. Violation of Primary Key " & _
"Constraint Occured. Enter an unique value.Please check row number : 1", MsgBoxStyle.Critical, "Error")
Else
MsgBox(ex.ToString)
End If
If ex.Number.Equals(208) Then
MsgBox("Invalid Table Name. Check the Sql String.", MsgBoxStyle.Critical, "Error")
End If

End If
If chkBox2_Crtusr.Checked Then
If ex.Number.Equals(2627) Then 'Exception code for Primary Key Violation
MsgBox("Duplicate Employee ID cannot be inserted. Violation of Primary Key " & _
"Constraint Occured. Enter an unique value.Please check row number : 2", MsgBoxStyle.Critical, "Error")
Else
MsgBox(ex.ToString)
End If
If ex.Number.Equals(208) Then
MsgBox("Invalid Table Name. Check the Sql String.", MsgBoxStyle.Critical, "Error")
End If


Now I am calling the above procedure

VB
Private Sub btnSubmit_CrtMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit_CrtMul.Click
Dim cnt As Integer = 0

Try

If chkBox1_Crtusr.Checked Then
rtrnQry(txtUsrNm_1.Text, txtPswd_1.Text, txtFName_1.Text, txtLName_1.Text, txtEmpID_1.Text, cmbProNm1.SelectedItem, cmbAdmin1.SelectedItem, 1)
Dim sqlLogin As String = "Select * from login where user_name ='" & txtUsrNm_1.Text & "'"
Dim usrVerified As String = DataStore.ExecuteScalar(sqlLogin, sqlConnStr)
If usrVerified.Length > 0 Then
Call ClearData(1)
cnt = cnt + 1
End If
End If

If chkBox2_Crtusr.Checked Then
rtrnQry(txtUsrNm_2.Text, txtPswd_2.Text, txtFName_2.Text, txtLName_2.Text, txtEmpID_2.Text, cmbProNm2.SelectedItem, cmbAdmin2.SelectedItem, 2)
Dim sqlLogin As String = "Select * from login where user_name ='" & txtUsrNm_2.Text & "'"
Dim usrVerified As String = DataStore.ExecuteScalar(sqlLogin, sqlConnStr)
If usrVerified.Length > 0 Then
Call ClearData(2)
cnt = cnt + 1
End If
End If

If chkBox3_Crtusr.Checked Then
rtrnQry(txtUsrNm_3.Text, txtPswd_3.Text, txtFName_3.Text, txtLName_3.Text, txtEmpID_3.Text, cmbProNm3.SelectedItem, cmbAdmin3.SelectedItem, 3)
Dim sqlLogin As String = "Select * from login where user_name ='" & txtUsrNm_3.Text & "'"
Dim usrVerified As String = DataStore.ExecuteScalar(sqlLogin, sqlConnStr)
If usrVerified.Length > 0 Then
Call ClearData(3)
cnt = cnt + 1
End If
End If


Now when I am doing this, for each exception the error message is appearing twice, I need to take care of that as well. Please Help, Thanks in advance
Posted
Updated 22-Jul-10 21:45pm
v2
Comments
Ankur\m/ 23-Jul-10 3:49am    
Put the Code Snippet inside PRE tags (Code Blocks).
I have formatted and re-tagged your question already.
Shining Legend 23-Jul-10 5:47am    
use switch case in catch block
souvikd 23-Jul-10 5:49am    
I need a better option, please some one help

It is pretty clear why you get the error twice. The code lines are added twice in the exception handler.

This part:

VB
If chkBox2_Crtusr.Checked Then
If ex.Number.Equals(2627) Then 'Exception code for Primary Key Violation
MsgBox("Duplicate Employee ID cannot be inserted. Violation of Primary Key " & _
"Constraint Occured. Enter an unique value.Please check row number : 2", MsgBoxStyle.Critical, "Error")
Else
MsgBox(ex.ToString)
End If
If ex.Number.Equals(208) Then
MsgBox("Invalid Table Name. Check the Sql String.", MsgBoxStyle.Critical, "Error")
End If
 
Share this answer
 
Comments
souvikd 23-Jul-10 7:36am    
I understand that but i need a better way to handle exception- other than hard coding it with the error codes which is my primary concern
E.F. Nijboer 24-Jul-10 16:38pm    
You could create a database table that you could use to lookup the message that you want to show with certain error codes but this would not help if the connection isn't working. You could also use a dll to store the error codes with the messages. But what is it that you want to be better? Can you give some kind of idea you would want?
Given that you wish to do so many exceptions, do not use the if statements.
Apart from making the code unfeasably longwinded, it is ugly and inefficient.

For best results put the exceptions in a case block, then call procs or methods or whatever, as per needed by the exception.
 
Share this answer
 
v2
Hi thanks for replying, actually i am trying to insert 15 different user details to the DB with my form,now if i use the above code - it works fine, but it includes lots of coding, is there any other way like creating some proc or something else to solve this, i m new to vb.net hence struggling a lot on these small things. Please help, thanks is advanced.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900