Hi all,

I'm trying to get some values from my database using data reader. Its my first time in using dataReader and I have this error

"Value of type 'System.Data.SqlClient.SqlDataReader' cannot be converted to 'String'."

My code

 Public Sub LogIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim connStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\sony\Documents\Visual Studio 2010\Projects\SADA\SADA\App_Data\SADA2.mdf';Integrated Security=True;User Instance=True"
        Dim sqlconnet As Data.SqlClient.SqlConnection
        Dim MyComm As Data.SqlClient.SqlCommand
        Dim dr As SqlDataReader
        Dim UserType As String
        Dim Pateint As String
        Dim SLP As String
        Dim Admin As String
        Dim logID As String
        Dim PassW As String

        sqlconnet = New Data.SqlClient.SqlConnection()
        sqlconnet.ConnectionString = connStr
        MyComm = New Data.SqlClient.SqlCommand("", sqlconnet)
        MyComm.CommandType = Data.CommandType.Text

        MyComm.CommandText = "SELECT * FROM LoginTable WHERE (loginId ='" & TextBox1.Text & "') AND (Password = '" & TextBox2.Text & "') "
        sqlconnet.Open()
        dr = MyComm.ExecuteReader()
        dr.Read()
        logID = dr[0].ToString() 
        PassW=dr[1].ToString()
        Admin=dr[2].ToString()
        SLP=dr[3].ToString()
        Pateint=dr[4].ToString()
        UserType=dr[5].ToString()




        Dim result As Data.SqlClient.SqlDataReader = MyComm.ExecuteReader(Data.CommandBehavior.CloseConnection)
        If result.HasRows = False Then
            MsgBox("Error")

        ElseIf result.HasRows = True And UserType = "S" Then
            Response.Redirect("SLPMain.aspx")
        ElseIf result.HasRows = True And UserType = "A" Then
            Response.Redirect("Admin.aspx")
        ElseIf result.HasRows = True And UserType = "P" Then
            Response.Redirect("WebForm4.aspx")

        End If
        result.Close()
    End Sub

Can anyone help me plz ?!!

Try using the getString() method of the reader with the column number. E.g.

logID = dr.getString(0)
PassW = dr.getString(1)

hi hericles, thanks for your replay

I used getString () but i get this error

Unable to cast object of type 'System.Int32' to type 'System.String'.

I think i fixed the error
I changed
logID = dr[0].ToString()

to

logID = dr.ToString(0)

but i got another error

"There is already an open DataReader associated with this Command which must be closed first."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.