ExecuteScalar don't return the corrent Int value.

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
 
W

William \(Bill\) Vaughn

When using equality (=) operators in the WHERE clause, the comparison has to
match exactly. I expect that the values supplied for first and last names
don't match for some reason. Are they in fact Unicode values? Is the
database installed as case sensitive and if so, does the case of the values
match the case of the data in the column? If the target column is a
fixed-length value, is the length exactly the same?
I would try a "fuzzy" comparison to debug this--use a LIKE expression on one
or both of the columns being tested.


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top