Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I haven't written code in quite a few years and I have been given a task at work to attempt at making a entry/exit logging program. I have to connect to a SQL database that contains names and badge numbers. The badge numbers are obtained through a magnetic swipe reader. I need to figure out how to read the card into the hidden textbox and then search the database and display the associated name on a monitor. This program also needs to remove the name if the card is swiped again. I also don't quite know how to check the listbox to determine if a name is already listed so that it can be removed. I don't expect someone to write the program for me, I'm just looking for a few pointers or some resources that might get me pointed in the right direction.

What I have tried:

I have been able to come up with code that will fill the listbox on load but I need it to be blank at load and only show names as the cards are swiped. I have tried a using WHERE within the SELECT statement but I don't know how to read the textbox and assign it to something that can be compared in the WHERE statement.

Imports System.Data.SqlClient

Public Class EmployeeList

    Public conn As SqlConnection = New SqlConnection("Server=;Database=;Trusted_Connection=True;")
    Public cmd As New SqlCommand
    Public da As New SqlDataAdapter
    Public ds As New DataSet



    Private Sub EmployeeList_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        conn.Open()

        Me.TextBox1.Text = "@BadgeNum"
        With cmd

            .Connection = conn
            .CommandText = "SELECT LastName FROM EmployeeData2 WHERE BadgeNumber = @BadgeNum;"
        End With

        da.SelectCommand = cmd

        da.Fill(ds, "EmployeeData2")

        With ListBox1
            .DataSource = ds.Tables(0)
            .DisplayMember = "LastName"
        End With

        conn.Close()

    End Sub
Posted
Comments
[no name] 17-May-17 13:15pm    
cmd.Parameters.AddWithValue("@BadgeNum", TextBox1.Text)
Mike S 19-May-17 17:35pm    
Thank you. This gets me going again.
RickZeeland 17-May-17 13:37pm    
Maybe this will be of interest to you, although it is in C#:
http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
you could bind the listbox to the textbox.
Mike S 19-May-17 17:36pm    
I'll check out the link. Thanks
Mike S 19-May-17 17:42pm    
Would going the route of using LINQ code make this easier or would it even work for what I need?

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