Results 1 to 5 of 5

Thread: DataReader And SQL Server

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Chesterfield, UK
    Posts
    298

    DataReader And SQL Server

    VB Code:
    1. Dim objConnection As New SqlClient.SqlConnection _
    2.                     ("server=.;database=ndbs;trusted_connection=true")
    3.  
    4.         Dim objCommand As New SqlClient.SqlCommand _
    5.             ("Select * From tblUsers", objConnection)
    6.  
    7.         Dim objReader As SqlClient.SqlDataReader
    8.  
    9.         ' Open the database connection
    10.         objConnection.Open()
    11.  
    12.         ' Put the results in the DataReader object
    13.         objReader = objCommand.ExecuteReader
    14.  
    15.         Do While objReader.Read  
    16.  
    17.             If objReader.Item("password") = "password" Then
    18.                 Response.Write("The Password Is Password")
    19.             End If
    20.  
    21.         Loop
    22.  
    23.         objReader.Close()
    24.         objConnection.Close()
    25.  
    26.  
    27.     End Sub

    The problem I have is that when I use the following :

    VB Code:
    1. If objReader.Item("password") = "password" Then
    2.                 Response.Write("The Password Is Password")
    3.             End If

    It just does not recognize that I am trying to compare the strings. It is so frustrating, if I do a response.write(objReader.Item("password")) whilst the datareader.read loop goes around then it does display the information that the DataReader pulls from the sql database.

    Is it because the DataReader is read-only????

    Any ideas?????

    Cheers,


    Matt.

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Try calling the ToString() method before doing your comparison:

    Code:
    objReader.Item("password").ToString() == "Password"
    Last edited by Lethal; Oct 22nd, 2003 at 01:28 PM.

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    you should be able to do something like this as well.
    Code:
    If objReader["password"] = "password" Then
       Response.Write("The Password Is Password")
    End If
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    The Microsoft-recommended way to do string comparisons is as follows:

    if objReader.Item("password").toLower.CompareTo( "password") = 0 then
    ...

    end if

    Note that this compares the CONTENTS of the column. If that's what you want, then good.

    If you were attempting to compare the column name, then you need to use (from memory, so I might be slightly off) objReader.columns("password").ColumnName
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Chesterfield, UK
    Posts
    298

    Thanks

    Thanks for you help, just out of interest I had to do the following :

    VB Code:
    1. If Trim(objReader["password"].ToString) = "password" Then
    2.    Response.Write("The Password Is Password")
    3. End If

    Weird I thought, having use Trim, when using MS Access I have never had to do that.

    Thanks a lot.


    Matt.

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