Click here to Skip to main content
15,891,846 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a strange promble here. I use SQL Compact Server and have a column called "StaffPass", type nvarchar lenght 16.

this is my function:

Friend Shared Function VerifyStaff(ByVal id As Integer, ByVal pass As String) As Boolean

      VerifyStaff = False

      Try
         Using ceconn As New SqlCeConnection(My.Settings.KMTDataConn)

            If ceconn.State = ConnectionState.Closed Then ceconn.Open()
            Dim trans = ceconn.BeginTransaction
            Using _
               cmd As _
                  New SqlCeCommand() _
                     With {.CommandType = CommandType.Text,
                        .CommandText =
                           String.Format(
                              "SELECT StaffID, StaffPass FROM StaffInfo  WHERE  StaffID = {0} AND StaffPass = {1}", id, pass),
                        .Connection = ceconn, .Transaction = trans}
               Dim recordCount As Long = cmd.ExecuteScalar
               If recordCount > 0 Then
                  VerifyStaff = True
                  Return VerifyStaff
               Else
                  VerifyStaff = False
                  Return VerifyStaff
               End If
            End Using
         End Using
      Catch ex As Exception
         Return VerifyStaff
      End Try

   End Function


This works fint is the password is "1111", however if the password is "K1234"
then it fails

Any Idea's

Thx
Posted
Comments
fsudsgaard 9-Jan-12 18:55pm    
Thank so much guys, the moment I saw it, I knew...guess I been looking at it too long

First, don't use string concatentation to build queries like this. Use parameterized queries instead. Just Google for "vb.net parameterized queries" for a ton of articles and examples.

Next, the way tou have this query string build, your SQL query will look like this when it's executed:
SELECT StaffID, StaffPass FROM StaffInfo WHERE StaffID = 0 AND StaffPass = K1234

NOtice anything wrong with that? If you want to match a string field, you have to wrap the string in quotes:
SELECT StaffID, StaffPass FROM StaffInfo WHERE StaffID = 0 AND StaffPass = 'K1234'

So, your query line would look like:
.CommandText =
    String.Format(
        "SELECT StaffID, StaffPass FROM StaffInfo  WHERE  StaffID = {0} AND StaffPass = '{1}'", id, pass),


But again, ditch this and use parameterized queries instead.
 
Share this answer
 
If the password is the string, you need to catch your password into (').

VB
"SELECT StaffID, StaffPass FROM StaffInfo  WHERE  StaffID = {0} AND StaffPass = '{1}'", id, pass)"
 
Share this answer
 
Comments
fsudsgaard 9-Jan-12 18:57pm    
Thanks for the help, it is working now
As already mentioned, always use parameters.

Another thing is that the code is a bit confusing. ExecuteScalar does not return the record count. It returns the first column of the first row in the result set (StaffID in this case).

Based on the code you don't seem to be interested in the actual values of StaffID and StaffPass so if that's true you could modify your query to:
SQL
SELECT COUNT(*) FROM StaffInfo WHERE StaffID = @staffid AND StaffPass = @staffpass

For more info about parameters, see: SqlCeParameter[^]

But if you want to return the values of StaffID and StaffPass, consider using SqlCeDataReader[^]
 
Share this answer
 
Comments
fsudsgaard 9-Jan-12 18:57pm    
Thanks for you help,accually already changed it..
Wendelius 10-Jan-12 0:02am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900