Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

ExecuteScalar doesn't return the corrent Int value.

pedestrianpedestrian Member Posts: 10
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.

[code]
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 from the following Message box statement ***
MsgBox("intEmpID:" & CStr(intEmployeeID))
End If
[/code]
Sign In or Register to comment.