Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi in my application i am trying to insert 15 diferrent user details to the DB, which looks like an excel sheet,for validation purpose i am checking if the emp id already exists in the DB and if so then i am showing a lable and disabling the checkbox,this works fine, but i am unable to do the same for handling IsNumeric, please check the code to understand it better
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)
                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


This way i m doing the same for next 13 users.

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("[dbo].[CreateUser]", 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

Now i m using Control Lost focus event to show the msg
Private Sub Controls_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmpID_1.LostFocus, txtEmpID_2.LostFocus, _
    txtEmpID_3.LostFocus, txtEmpID_4.LostFocus, txtEmpID_5.LostFocus, txtEmpID_6.LostFocus, txtEmpID_7.LostFocus, _
    txtEmpID_8.LostFocus, txtEmpID_9.LostFocus, txtEmpID_10.LostFocus, txtEmpID_11.LostFocus, _
    txtEmpID_12.LostFocus, txtEmpID_13.LostFocus, txtEmpID_14.LostFocus, txtEmpID_15.LostFocus
        Try
            If txtEmpID_1.TextLength > 0 Then
                If rtrnLngth(txtEmpID_1.Text) = 0 Then
                    lblsts1.Text = "Available"
                    lblsts1.ForeColor = Color.LimeGreen
                    If chkBox1_Crtusr.Enabled = False Then chkBox1_Crtusr.Enabled = True
                Else
                    lblsts1.Text = "Not available"
                    lblsts1.ForeColor = Color.Red
                    If chkBox1_Crtusr.Checked Then
                        chkBox1_Crtusr.Enabled = False
                        chkBox1_Crtusr.CheckState = CheckState.Unchecked
                    End If
                End If
            End If
            If txtEmpID_2.TextLength > 0 Then
                If rtrnLngth(txtEmpID_2.Text) = 0 Then
                    lblsts2.Text = "Available"
                    lblsts2.ForeColor = Color.LimeGreen
                    If chkBox2_Crtusr.Enabled = False Then chkBox2_Crtusr.Enabled = True
                Else
                    lblsts2.Text = "Not available"
                    lblsts2.ForeColor = Color.Red
                    If chkBox2_Crtusr.Checked Then
                        chkBox2_Crtusr.CheckState = CheckState.Unchecked
                        chkBox2_Crtusr.Enabled = False
                    End If


doing same for 13 user...this works fine ....i need to show the same kind of msg for empty fields and for IsNumeric. Please help with an idea
Posted

1 solution

A few remarks :

1 you're leaving yourself open for a SQL injection attack
Dim sqlLogin As String = "Select * from login where user_name ='" & txtUsrNm_1.Text & "'"


2 Your code is duplicated everywhere. There a a few options
- You could implement a kind of control array, read http://visualbasic.about.com/od/usingvbnet/l/bldykctrlarraya.htm[^]

- Refactor the code :
Private Sub CheckEmpID (TxtEmpID as TextBox, lblsts as Label,chkBox as CheckBox)
            If txtEmpID.TextLength > 0 Then
                If rtrnLngth(txtEmpID.Text) = 0 Then
                    lblsts.Text = "Available"
                    lblsts.ForeColor = Color.LimeGreen
                    chkBox.Enabled = True
                Else
                    lblsts.Text = "Not available"
                    lblsts.ForeColor = Color.Red
                    chkBox.Enabled = False
                    chkBox.CheckState = CheckState.Unchecked
                End If
            End If
end sub


Call CheckEmpID (TxtEmpID1, lblsts_1, chkBox1_Crtusr) etc.. from within Controls_LostFocus
Do the same for btnSubmit_CrtMul_Click


Cheers
 
Share this answer
 
v2

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