Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I want to read employee information from the employee table and check if the item is stored in the attendance table with the day, barcode and name entries before storing it in the database. When you create the following code, the data is stored twice at the same time.

What I have tried:

Dim command As SqlCommand
        command = New SqlCommand("SELECT COUNT(EmpName) as EmpNameCount FROM EmployeesAttend", con)
        Dim reader As SqlDataReader = command.ExecuteReader()
        '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        While (reader.Read())
            If reader("EmpNameCount") = 0 Then
                'حالة عدم وجود بيانات
                'حفظ البيانات مباشرة
                GetEmpInfo()
                SaveEmpInfATT()
                Clear()

            Else
                'حالة وجود بيانات
                ' التأكد من عدم وجود ازدواجية
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                con = New SqlConnection(cs)
                con.Open()
                Dim cmd11 As New SqlCommand(" Select EmpCode from EmployeesAttend Where EmpCode = '" & TxtCode.Text.Trim & "' and AttendsDay='" & Label8.Text.Trim & "' and AttendsDate = '" & Label5.Text.Trim & "'  ", con)
                Dim dr1 As SqlDataReader = cmd11.ExecuteReader()
                '=================================
                If dr1.Read Then
                    If MsgBox(" الموظف  تم تسجيلة من قبل و لا يمكن تسجيلة مره اخرى", MsgBoxStyle.Exclamation, "منظومة الدمرداش المدرسية") = MsgBoxResult.Ok Then
                        con.Close()
                        TxtCode.SelectAll()
                        Exit Sub
                    End If
                Else
                    GetEmpInfo()
                    SaveEmpInfATT()
                    Clear()

                End If
                '=======================================
            End If
        End While
Posted
Updated 27-Jan-18 8:22am

1 solution

VB
Dim cmd11 As New SqlCommand(" Select EmpCode from EmployeesAttend Where EmpCode = '" & TxtCode.Text.Trim & "' and AttendsDay='" & Label8.Text.Trim & "' and AttendsDate = '" & Label5.Text.Trim & "'  ", con)

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
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