Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code below is 100% correct and fetches data from the database. It also fetches data from the form and then places it in List View when copying the code into a textbox. However, when using the bar code reader, the user code does not appear and the username does not appear on the list. Is the problem in the event of Text changed or what?


What I have tried:

<pre>   If Len(Trim(CmbWTime.Text)) = 0 Then
            MessageBox.Show("من فضلك حدد فترة الدوام", "منظومة الدمرداش المدرسية", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            CmbWTime.Focus()
            Exit Sub
        End If
        con = New SqlConnection(cs)
        con.Open()
        Dim strQ As String = String.Empty
        Dim cmd0 As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        strQ = " Select EmpCode,EmpName from Employees where EmpCode='" + TxtCode.Text + "'"
        cmd0 = New SqlCommand(strQ, con)
        da = New SqlDataAdapter(cmd0)
        ds = New DataSet
        da.Fill(ds, "Employees")
        If ds.Tables(0).Rows.Count > 0 Then
            txtEmpCode.Text = ds.Tables(0).Rows(0)("EmpCode").ToString()
            TxtEmpName.Text = ds.Tables(0).Rows(0)("EmpName").ToString()
        End If

        If TeMAL.Text = "لم يبدأ الدوام" Then
            Exit Sub
        ElseIf TeMAL.Text = "حضـر الموظف" Then
            'كود الإضافة إلى الليست فيو
            Dim temp As Integer
            temp = ListView1.Items.Count()
            If temp = 0 Then
                Dim i As Integer
                Dim lst As New ListViewItem(i)
                lst.SubItems.Add(txtEmpCode.Text) 'كود الموظف
                lst.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
                lst.SubItems.Add(Label9.Text) 'وقت الحضور
                lst.SubItems.Add("بالدوام") 'وقت الإنصراف
                lst.SubItems.Add(TeMAL.Text) 'حالة الموظف
                lst.SubItems.Add(Label5.Text) ' تاريخ الحضور
                lst.SubItems.Add(Label8.Text) ' يوم الحضور
                lst.SubItems.Add(CmbWTime.Text) ' فترة الحضور
                lst.SubItems.Add(RichTextBox1.Text) ' ملاحظات
                lst.SubItems.Add("حضر بالميعاد") ' حالة الغياب
                lst.SubItems.Add(LateTime.Text) ' معدل التأخير
                ListView1.Items.Add(lst)
                i = i + 1
                TxtCode.SelectAll()
                txtEmpCode.Text = ""
                TxtEmpName.Text = ""
                Exit Sub
            End If
            For j = 0 To temp - 1
                If (ListView1.Items(j).SubItems(1).Text = txtEmpCode.Text) And (ListView1.Items(j).SubItems(6).Text = Label5.Text) Then
                    If MessageBox.Show("هذا الموظف لايمكن تسجيلة مرتين", "منظومة الدمرداش المدرسية", MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
                        TxtCode.Clear()
                        TxtCode.Focus()
                    End If
                    Exit Sub
                End If
            Next j
            Dim k As Integer
            Dim lst1 As New ListViewItem(k)
            lst1.SubItems.Add(txtEmpCode.Text) 'كود الموظف
            lst1.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
            lst1.SubItems.Add(Label9.Text) 'وقت الحضور
            lst1.SubItems.Add("بالدوام") 'وقت الإنصراف
            lst1.SubItems.Add(TeMAL.Text) 'حالة الموظف
            lst1.SubItems.Add(Label5.Text) ' تاريخ الحضور
            lst1.SubItems.Add(Label8.Text) ' يوم الحضور
            lst1.SubItems.Add(CmbWTime.Text) ' فترة الحضور
            lst1.SubItems.Add(RichTextBox1.Text) ' ملاحظات
            lst1.SubItems.Add("حضر بالميعاد") ' حالة الغياب
            lst1.SubItems.Add(LateTime.Text) ' معدل التأخير
            ListView1.Items.Add(lst1)
            k = k + 1
            TxtCode.SelectAll()
            txtEmpCode.Text = ""
            TxtEmpName.Text = ""

        ElseIf TeMAL.Text = "تأخر الموظف" Then
            'كود الإضافة إلى الليست فيو
            Dim temp As Integer
            temp = ListView1.Items.Count()
            If temp = 0 Then
                Dim i As Integer
                Dim lst As New ListViewItem(i)
                lst.SubItems.Add(txtEmpCode.Text) 'كود الموظف
                lst.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
                lst.SubItems.Add(Label9.Text) 'وقت الحضور
                lst.SubItems.Add("بالدوام") 'وقت الإنصراف
                lst.SubItems.Add(TeMAL.Text) 'حالة الموظف
                lst.SubItems.Add(Label5.Text) ' تاريخ الحضور
                lst.SubItems.Add(Label8.Text) ' يوم الحضور
                lst.SubItems.Add(CmbWTime.Text) ' فترة الحضور
                lst.SubItems.Add(RichTextBox1.Text) ' ملاحظات
                lst.SubItems.Add("حضر متأخرا") ' حالة الغياب
                lst.SubItems.Add(LateTime.Text) ' معدل التأخير
                ListView1.Items.Add(lst)
                i = i + 1
                TxtCode.SelectAll()
                txtEmpCode.Text = ""
                TxtEmpName.Text = ""
                Exit Sub
            End If
            For j = 0 To temp - 1
                If (ListView1.Items(j).SubItems(1).Text = txtEmpCode.Text) And (ListView1.Items(j).SubItems(6).Text = Label5.Text) Then
                    MessageBox.Show("هذا الموظف لايمكن تسجيلة مرتين", "منظومة الدمرداش المدرسية", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    TxtCode.SelectAll()
                    Exit Sub
                End If
            Next j
            Dim k As Integer
            Dim lst1 As New ListViewItem(k)
            lst1.SubItems.Add(txtEmpCode.Text) 'كود الموظف
            lst1.SubItems.Add(TxtEmpName.Text) 'اسم الموظف
            lst1.SubItems.Add(Label9.Text) 'وقت الحضور
            lst1.SubItems.Add("بالدوام") 'وقت الإنصراف
            lst1.SubItems.Add(TeMAL.Text) 'حالة الموظف
            lst1.SubItems.Add(Label5.Text) ' تاريخ الحضور
            lst1.SubItems.Add(Label8.Text) ' يوم الحضور
            lst1.SubItems.Add(CmbWTime.Text) ' فترة الحضور
            lst1.SubItems.Add(RichTextBox1.Text) ' ملاحظات
            lst1.SubItems.Add("حضر متأخرا") ' حالة الغياب
            lst1.SubItems.Add(LateTime.Text) ' معدل التأخير
            ListView1.Items.Add(lst1)
            k = k + 1
            TxtCode.SelectAll()
            txtEmpCode.Text = ""
            TxtEmpName.Text = ""

        ElseIf TeMAL.Text = "إنتهى الدوام" Then
            Exit Sub
        End If
        TxtCode.Clear()
        TxtCode.Focus()
Posted
Updated 7-Feb-18 10:18am

Quote:
The code below is 100% correct and fetches data from the database.

No it is not.
VB
strQ = " Select EmpCode,EmpName from Employees where EmpCode='" + TxtCode.Text + "'"

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
 
That code is nowhere near "100% correct". It's dangerous, and risks your database every time a user runs it.

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

When you have gone through your entire app and fixed all occurrences of that, use the debugger to look at exactly what is going on while you run your app. Since the problem depends on data that we do not have any access to, we can't do that for you - so, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
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