P
pedestrian
I'm using VB ADO.NET with Northwind database Employees table. I create a
WinForm with
a ComboBox (cboEmployee) to let user to select or enter full name and store
it in strNames string array.
I then try to get the matching Employee ID based on the employee name entered.
However the ***SqlCommand.ExecuteScalar*** don't return the corrent
EmployeeID... (refer coding)
What is the possible mistake?
Thanks for your assistance.
------------------------------------------------------------
Dim intEmployeeID As Integer = 0
Dim strNames(1) As String
Dim strInput As String = cboEmployee.Text.Trim
'If user select an item from list or type valid Employee
If cboEmployee.SelectedIndex <> -1 OrElse cboEmployee.FindStringExact
(strInput) <> -1 Then
'The names in cboEmployee is in following format "FirstName, LastName"
strNames = strInput.Split(", ")
Dim cmdGetEmpID As New SqlCommand("SELECT EmployeeID FROM Employees
WHERE FirstName=@firstName And LastName=@lastName", conn)
cmdGetEmpID.Parameters.Add("@firstName", SqlDbType.NVarChar, 20)
cmdGetEmpID.Parameters("@firstName").Value = strNames(0)
cmdGetEmpID.Parameters.Add("@lastName", SqlDbType.NVarChar, 40)
cmdGetEmpID.Parameters("@lastName").Value = strNames(1)
'*** Problem with following statement ***
intEmployeeID = CInt(cmdGetEmpID.ExecuteScalar())
'*** I always get 0 return from the following statement ***
MsgBox("intEmpID:" & CStr(intEmployeeID))
End If
WinForm with
a ComboBox (cboEmployee) to let user to select or enter full name and store
it in strNames string array.
I then try to get the matching Employee ID based on the employee name entered.
However the ***SqlCommand.ExecuteScalar*** don't return the corrent
EmployeeID... (refer coding)
What is the possible mistake?
Thanks for your assistance.
------------------------------------------------------------
Dim intEmployeeID As Integer = 0
Dim strNames(1) As String
Dim strInput As String = cboEmployee.Text.Trim
'If user select an item from list or type valid Employee
If cboEmployee.SelectedIndex <> -1 OrElse cboEmployee.FindStringExact
(strInput) <> -1 Then
'The names in cboEmployee is in following format "FirstName, LastName"
strNames = strInput.Split(", ")
Dim cmdGetEmpID As New SqlCommand("SELECT EmployeeID FROM Employees
WHERE FirstName=@firstName And LastName=@lastName", conn)
cmdGetEmpID.Parameters.Add("@firstName", SqlDbType.NVarChar, 20)
cmdGetEmpID.Parameters("@firstName").Value = strNames(0)
cmdGetEmpID.Parameters.Add("@lastName", SqlDbType.NVarChar, 40)
cmdGetEmpID.Parameters("@lastName").Value = strNames(1)
'*** Problem with following statement ***
intEmployeeID = CInt(cmdGetEmpID.ExecuteScalar())
'*** I always get 0 return from the following statement ***
MsgBox("intEmpID:" & CStr(intEmployeeID))
End If