Results 1 to 13 of 13

Thread: [RESOLVED] ExecuteScalar tricking me.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [RESOLVED] ExecuteScalar tricking me.

    I want to loop through my table and extract a UserName that corresponds with the UserName typed in a text box using the execute scarlar method.

    If it's there then i call a message and exit the sub, if its not there then i add the User.

    VB Code:
    1. Public Shared Function LookUp( ByVal strSQL As String) As String
    2.         Dim strValue As String = ""
    3.         Dim myConn As OleDbConnection = New OleDbConnection(strAccessConn)
    4.         Dim myCommand As New OleDbCommand(strSQL, myConn)
    5.         Try
    6.             myConn.Open()
    7.             strValue = CStr(myCommand.ExecuteScalar()) 'Option strict is on
    8.             myConn.Close()
    9.         Catch ex As Exception
    10.             MsgBox("Error:" & ex.Message)
    11.         End Try
    12.         Return strValue
    13.     End Function

    Here is my SQL
    VB Code:
    1. Dim  strUserName as string="SELECT UserName FROM Users WHERE(UserName='" & Me.tbUserName.text.Trim() & "')"

    My Execute scalar returns a string but i cant get the string when i call it like this. It Returns an empty messagebox

    VB Code:
    1. MessageBox.Show(LookUp(strUserName).toUpper)
    Last edited by maps; Jun 23rd, 2006 at 03:56 AM.

  2. #2
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: ExecuteScalar tricking me.

    I think you should remove the first call to ExecuteScalar method, anyway did you try the query if it really returns a row?
    live, code and die...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: ExecuteScalar tricking me.

    Yap. it returns a row. I tried it in the access db it self.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: ExecuteScalar tricking me.

    Errr.... sorry. the First executeScalar shouldnt be there. It was a typo.
    But the problem still stands

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: ExecuteScalar tricking me.

    The Error is Object Reference not set to an Instance Of an Object...!!!

  6. #6
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: ExecuteScalar tricking me.

    Can you paste the code that you really have.
    live, code and die...

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: ExecuteScalar tricking me.

    I Edited the one above and thats the one i have.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: ExecuteScalar tricking me.

    How about using this:
    VB Code:
    1. Dim myCommand As New OleDbCommand("SELECT COUNT(*) FROM Users WHERE UserName = @UserName", myConnection)
    2.  
    3. myCommand.Parameters.AddWithValue("@UserName", Me.tbUserName.Text.Trim())
    4.  
    5. Try
    6.     myConnection.Open()
    7.  
    8.     If CInt(myCommand.ExecuteScalar()) = 0 Then
    9.         'No user with that username.
    10.     End If
    11. Finally
    12.     'You MUST close the connection in a Finally block or it will not be closed if an exception is thrown.
    13.     myConnection.Close()
    14. End Try
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: ExecuteScalar tricking me.

    Well there goes a good suggestion and I couldn't agree more
    live, code and die...

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: ExecuteScalar tricking me.

    Works ok. But how can i handle the validation.... ToUpper part of it.
    In case the user name in the table is lowercase and the person enters the same username but in Uppercase. it will show that it isnt there!!

    With my LookUp function i would do it like this.
    VB Code:
    1. If Not connMger.LookUp(strDupUserName).ToUpper = Trim(Me.tbUserName.Text).ToUpper Then
    2. MessageBox.Show("UserName Exists")
    3. Exit Sub
    4. Else
    5. AddUser()
    6. End If
    Last edited by maps; Jun 23rd, 2006 at 04:43 AM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: ExecuteScalar tricking me.

    If queries are case sensitive then you can do this:
    VB Code:
    1. Dim myCommand As New OleDbCommand("SELECT COUNT(*) FROM Users WHERE UPPER(UserName) = @UserName", myConnection)
    2.  
    3. myCommand.Parameters.AddWithValue("@UserName", Me.tbUserName.Text.Trim().ToUpper())
    4.  
    5. Try
    6.     myConnection.Open()
    7.  
    8.     If CInt(myCommand.ExecuteScalar()) = 0 Then
    9.         'No user with that username.
    10.     End If
    11. Finally
    12.     'You MUST close the connection in a Finally block or it will not be closed if an exception is thrown.
    13.     myConnection.Close()
    14. End Try
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: ExecuteScalar tricking me.

    Thanks but it doesnt work out.
    The Error Messager is, undefined function UPPER in Expression.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: ExecuteScalar tricking me.

    Hmmm... That was T-SQL (SQL Server) and unfortunately Jet SQL, which you will be using if your database is Access, does not support UPPER. Interestingly it does have a LOWER reserved word, but I'm not sure if it will convert a string to lower case. You could try changing UPPER to LOWER and ToUpper to ToLower and see if it works. if not then it might be worth a look through the Jet SQL reference to see if there is something available that will do the job.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width