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

--
Message posted via http://www.dotnetmonster.com

Re: ExecuteScalar don't return the corrent Int value. by William

William
Fri Aug 25 10:53:44 CDT 2006

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.
__________________________________

"pedestrian" <u16758@uwe> wrote in message news:6549de9a85f93@uwe...
> 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
>
> --
> Message posted via http://www.dotnetmonster.com
>



Re: ExecuteScalar don't return the corrent Int value. by pedestrian

pedestrian
Fri Aug 25 18:48:23 CDT 2006

Thanks for replying and valuable advice... William.

I have found the problematic statement : String.Split(", ")
and replace it with String.Split(",") follow by String.Trim()

Regards,

--
Regards,
Pedestrian, Penang.

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-ado-net/200608/1