Click to See Complete Forum and Search --> : No data exists for the row/column


jaklotz
August 18th, 2005, 08:15 PM
Hi - can anyone help with this?

Here is my code on my codebehind page that tries to check a user's login:

Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click
'perform database check here
Dim sPassword$, sUserName$
Dim cn As New OleDbConnection(Session("SQLConn"))
Dim cmd As New OleDbCommand
Dim dr As OleDbDataReader
cn.Open()
cmd.Connection = cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "UsersLoginCheck"
cmd.Parameters.Add("@UserName", txtUserName.Text)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dr.Read()
If dr.Read() Then
lblLoginMessage.Text = "Invalid Username!"
sPassword = dr("Password")
If txtPassword.Text = sPassword Then
Session("gblUserID") = dr("UserID")
Session("gblUserName") = dr("UserName")
Else
lblLoginMessage.Text = "Invalid Password!"
End If
Else
lblLoginMessage.Text = "Invalid Username!"
End If
dr.Close()
cn.Close()
End Sub

The problem is, even though I know the data exists in the table in the db, the data reader doesn't have it - I get this in my watch window:

dr("UserID") Run-time exception thrown : System.InvalidOperationException - No data exists for the row/column.

I know a record exists for my username that I passed in - why do I get this error??
HELP!!

aquafin
August 18th, 2005, 11:32 PM
try using while(dr.read()) instead of 'if statement'


or alternatively you can use the code below, write a function in teh login button click event ,which returns boolean value true when a user with matching id and password are found. Based on the return value you can toggle the visiblity of the lblloginmessage label.




Public Sub BtnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogin.Click

If MyCustomMethod(txtUserName.Text, txtPassword.Text) Then

Lblloginmessage.Visible = "false"
Else

Lblloginmessage.Visible = "true"

End If
End Sub

Function MyCustomMethod(ByVal strUsername As String, _
ByVal strPassword As String) As Boolean

' Open DB
' ' Run sproc that returns UserID
' Return True if UserID found else False
'Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("connString"))

Dim ConnectsUserMaster As New Elearn_ClsLib.Connection
Dim dreader
Try
dreader = ConnectsUserMaster.GetDataReader("Select UserMaster_FirstName from Elearn_UserMaster where UserMaster_Userid='" & strUsername & "' and UserMaster_Pwd='" & strPassword & "' )
While (dreader.Read())

Return True

End While
end function

hope this helps!

cmiskow
August 21st, 2005, 09:37 AM
I'm not sure if you are aware, but dr.Read() consumes a row. So in this bit of code:
dr.Read()
If dr.Read() Then
you are actually attempting to read the second row, which most likely doesn't exist. Since you said the watch shows an exception, I'm guessing the code is executing normally and that the If statement is failing. This is because the datareader has already reached the end of the results due to the previous Read() call.

jaklotz
August 22nd, 2005, 11:24 AM
I didn't know it read automatically reads the row - that was the problem. Thanks!