I am trying to draw random employees from a mSQL database with a button press any needed amount of times. The first button press returns a random employee with no issue but the second or any thereafter just return my exception errors. I know it has to something really simple I am missing and I an very new to programming with VB or anything for that matter. Below is the code I am trying to make function and any suggestions are very much appreciated.
Private Sub btnDrawEmployee_Click(sender As Object, e As EventArgs) Handles btnDrawEmployee.Click
' * Randomly draw a employee number from the database
If txbSetSessionNumber.Text = "" Then
' * Do nothing
lblEnterValidEmployeeID.Visible = True
lblEnterValidEmployeeID.Text = "PLEASE ENTER/CHECK SWIPE SESSION NUMBER."
Else
Try
sqlConn.Open()
lblEnterValidEmployeeID.Visible = False
Dim time = Date.Today
Dim cmd As SqlCommand = New SqlCommand(("SELECT TOP 1 * FROM Swipe_Data WHERE Session =" + txbSetSessionNumber.Text + "AND Date =" + "'" + time + "'" + "AND Drawn IS null ORDER BY NewID()"), sqlConn)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable
da.Fill(dt)
dgvDrawnEmployee.DataSource = dt
' * Pull data from GridView to label
lblEmployeeNumber.Text = dt.Rows(0)("EmpNumber").ToString.Trim
lblEmployeeName.Text = dt.Rows(0)("EmpName").ToString.Trim
lblEmployeeDepartment.Text = dt.Rows(0)("EmpDepartment").ToString.Trim
lblDrawDate.Text = dt.Rows(0)("Date").ToString
'
'dt.Clear()
'da = Nothing
'dgvDrawnEmployee.DataSource = Nothing
'dgvDrawnEmployee.Rows.Clear()
'dgvDrawnEmployee.Refresh()
' * Mark employee as drawn
drawnEmployee()
Catch ex As Exception
lblNoData1.Text = "No employees have been scanned/swiped in matching the session"
lblNoData2.Text = "criteria entered above! Please enter a valid session number."
lblNoData3.Text = "IT info - " + ex.Message
End Try
End If
End Sub
Aucun commentaire:
Enregistrer un commentaire