Hi, in my application i am inserting 15 different user details to the DB, the form looks like an excel sheet, i am able to handle duplicate key violation error, and to that i am doing the following
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 And IsNumeric(txtEmpID_1.Text) Then
rtrnQry(txtUsrNm_1.Text, txtPswd_1.Text, txtFName_1.Text, txtLName_1.Text, txtEmpID_1.Text, cmbProNm1.SelectedItem, cmbAdmin1.SelectedItem, 1)
If empIdSts(txtEmpID_1) = True Then
Call ClearData(1)
cnt = cnt + 1
End If
ElseIf Not IsNumeric(txtEmpID_1.Text) And txtEmpID_1.TextLength > 0 Then
Call isNumSts(lblsts1, chkBox1_Crtusr)
End If
If chkBox2_Crtusr.Checked And IsNumeric(txtEmpID_2.Text) Then rtrnQry(txtUsrNm_2.Text, txtPswd_2.Text, txtFName_2.Text,txtLName_2.Text, txtEmpID_2.Text,cmbProNm2.SelectedItem, cmbAdmin2.SelectedItem, 2)
If empIdSts(txtEmpID_2) = True Then
Call ClearData(2)
cnt = cnt + 1
End If
ElseIf Not IsNumeric(txtEmpID_2.Text) And txtEmpID_2.TextLength > 0 Then
Call isNumSts(lblsts2, chkBox2_Crtusr)
End If
I am doing this for all 15 users, using the following code i m handing duplicate key value
Public Sub chkEmpIDSts(ByVal txtbox As TextBox, ByVal lblSts As Label, ByVal chkBox As CheckBox)
Try
Dim empIDLngth As String = ""
Using conn As New SqlConnection(sqlConnStr)
conn.Open()
Using cmd As New SqlCommand("rtnLngth", conn)
With cmd
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue("@empid", txtbox.Text)
empIDLngth = .ExecuteScalar()
End With
If txtbox.TextLength > 0 Then
If empIDLngth = 0 Then
lblSts.Text = "Available"
lblSts.ForeColor = Color.LimeGreen
If chkBox.Enabled = False Then chkBox.Enabled = True
Else
lblSts.Text = "Not available"
lblSts.ForeColor = Color.Red
If chkBox.Checked Then
chkBox.CheckState = CheckState.Unchecked
chkBox.Enabled = False
End If
Every thing is going good till now, but my question is,if some one enters the same new empId twice for two different user, than my code wont be able to handle it as it checks if the empid is already thr in DB, and user will be able to insert it - hence will again get a duplicate key violation error. Please post me an idea to resolve the problem.Thanks