Click here to Skip to main content
16,014,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

See,
There are two way. Either you handle through front end or you do it while inserting data in the database.

If Exists(select * From TABNAME WHERE empid='e01')

In your CRUD, you can specify this before you insert the employee.

In your frontend, you can also check whether the this id is in the employee list already in the database.

Just select empid from TABNAME and find from the list that you create while user try to enter data from the UI.
 
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