jmar_click
asked on
IndexOutOfRangeException
Hi Experts,
I'm having a weird problem that I can't explain. I am trying to validate a user from the log in form. To do this I call a user class and run the validation function. But when I try to check if the user's status is set to 1 I get a IndexOutOfRangeException error.
Here is the full error:
Server Error in '/' Application.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
status
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeExce ption: status
Source Error:
Line 98: 'Make sure user is active
Line 99:
Line 100: If (rs("status") <> 1) Then
Line 101: Return False
Line 102: End If
Source File: e:\inetpub\wwwroot\App_Cod e\User.vb Line: 100
Stack Trace:
[IndexOutOfRangeException: status]
System.Data.ProviderBase.F ieldNameLo okup.GetOr dinal(Stri ng fieldName) +1358263
System.Data.SqlClient.SqlD ataReader. GetOrdinal (String name) +102
System.Data.SqlClient.SqlD ataReader. get_Item(S tring name) +12
User.ValidateUser() in e:\inetpub\wwwroot\App_Cod e\User.vb: 100
_Default.btn_login_Click(O bject sender, EventArgs e) in e:\inetpub\wwwroot\Default .aspx.vb:3 6
System.Web.UI.WebControls. Button.OnC lick(Event Args e) +105
System.Web.UI.WebControls. Button.Rai sePostBack Event(Stri ng eventArgument) +107
System.Web.UI.WebControls. Button.Sys tem.Web.UI .IPostBack EventHandl er.RaisePo stBackEven t(String eventArgument) +7
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData) +33
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +5102
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42
Here is my class: (disregard loaduser, count users, and display users)
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Data
Imports System.Data.Sql
Imports Microsoft.VisualBasic
Public Class User
Private _conn As SqlConnection
'Variables for auditor
Private _userId As Integer
Private _userName As String
Private _password As String
Private _csa As String
Private _status As Integer
'Connection Property
Public WriteOnly Property conn() As SqlConnection
Set(ByVal Value As SqlConnection)
_conn = Value
End Set
End Property
Public Property userId() As Integer
Get
Return _userId
End Get
Set(ByVal value As Integer)
_userId = value
End Set
End Property
Public Property userName() As String
Get
Return _userName
End Get
Set(ByVal Value As String)
_userName = Value
End Set
End Property
Public Property password() As String
Get
Return _password
End Get
Set(ByVal Value As String)
_password = Value
End Set
End Property
Public Property csa() As String
Get
Return _csa
End Get
Set(ByVal Value As String)
_csa = Value
End Set
End Property
Public Property status() As String
Get
Return _status
End Get
Set(ByVal Value As String)
_status = Value
End Set
End Property
Public Sub New()
init()
End Sub
Private Sub init()
_userId = -1
_userName = ""
_password = ""
_csa = ""
_status = 1
End Sub
Public Function ValidateUser() As Boolean
Dim cmd As New SqlCommand("sp_validateUse r", _conn)
_conn.Open()
Dim rs As SqlDataReader
cmd.CommandType = Data.CommandType.StoredPro cedure
cmd.Parameters.Add("@userN ame", Data.SqlDbType.VarChar, 50).Value = _userName
cmd.Parameters.Add("@passw ord", Data.SqlDbType.VarChar, 50).Value = _userName
rs = cmd.ExecuteReader()
If rs.Read() Then
'Make sure user is active
If (rs("status") <> 1) Then
Return False
End If
_userId = rs("userId")
_userName = rs("userName")
_csa = rs("csa")
_status = rs("status")
rs.Close()
_conn.Close()
Return True
Else
rs.Close()
_conn.Close()
Return False
End If
rs.Close()
_conn.Close()
End Function
Public Function LoadUser(ByVal userId As Integer)
Dim rs As SqlDataReader
Dim cmd As New SqlCommand("sp_selectUserD etail", _conn)
cmd.CommandType = Data.CommandType.StoredPro cedure
cmd.Parameters.Add("@userI d", Data.SqlDbType.Int).Value = userId
rs = cmd.ExecuteReader()
If rs.Read() Then
_userId = rs("userId")
_userName = rs("userName")
_csa = rs("csa")
_status = rs("status")
Return True
Else
Return False
End If
rs.Close()
End Function
Public Function DisplayUsers()
Dim rs As SqlDataReader
Dim cmd As New SqlCommand("sp_selectActiv eUsers", _conn)
cmd.CommandType = Data.CommandType.StoredPro cedure
rs = cmd.ExecuteReader()
While rs.Read()
_userId = rs("userId")
_userName = rs("userName")
_csa = rs("csa")
_status = rs("status")
End While
rs.Close()
Return Nothing
End Function
Public Function CountUsers(ByVal filterID As Integer)
Dim rs As SqlDataReader
Dim count As String = "0"
Dim thisFilter As Integer = CInt(filterID)
'set filter default
If thisFilter = 0 Then
thisFilter = 1
End If
'Open Database Connection
Dim conn As New SqlConnection(System.Confi guration.C onfigurati onManager. AppSetting s("conn"))
conn.Open()
Dim cmd As New SqlCommand("sp_Count_Users ", conn)
cmd.CommandType = Data.CommandType.StoredPro cedure
cmd.Parameters.Add("@UserS tatusID", Data.SqlDbType.Int).Value = thisFilter
rs = cmd.ExecuteReader()
If rs.Read() Then
'User Found
count = rs("getCount")
End If
conn.Close()
Return count
End Function
End Class
Here is how I am calling te class:
Dim user As New User
Dim conn As New SqlConnection(System.Confi guration.C onfigurati onManager. AppSetting s("conn"))
user.conn = conn
user.userName = username
user.password = password
If (user.ValidateUser() = True) Then
Response.Write("user passed")
Else
Response.Write("user failed")
End If
Thanks in advance
-Javier
I'm having a weird problem that I can't explain. I am trying to validate a user from the log in form. To do this I call a user class and run the validation function. But when I try to check if the user's status is set to 1 I get a IndexOutOfRangeException error.
Here is the full error:
Server Error in '/' Application.
--------------------------
status
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeExce
Source Error:
Line 98: 'Make sure user is active
Line 99:
Line 100: If (rs("status") <> 1) Then
Line 101: Return False
Line 102: End If
Source File: e:\inetpub\wwwroot\App_Cod
Stack Trace:
[IndexOutOfRangeException:
System.Data.ProviderBase.F
System.Data.SqlClient.SqlD
System.Data.SqlClient.SqlD
User.ValidateUser() in e:\inetpub\wwwroot\App_Cod
_Default.btn_login_Click(O
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
--------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42
Here is my class: (disregard loaduser, count users, and display users)
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Data
Imports System.Data.Sql
Imports Microsoft.VisualBasic
Public Class User
Private _conn As SqlConnection
'Variables for auditor
Private _userId As Integer
Private _userName As String
Private _password As String
Private _csa As String
Private _status As Integer
'Connection Property
Public WriteOnly Property conn() As SqlConnection
Set(ByVal Value As SqlConnection)
_conn = Value
End Set
End Property
Public Property userId() As Integer
Get
Return _userId
End Get
Set(ByVal value As Integer)
_userId = value
End Set
End Property
Public Property userName() As String
Get
Return _userName
End Get
Set(ByVal Value As String)
_userName = Value
End Set
End Property
Public Property password() As String
Get
Return _password
End Get
Set(ByVal Value As String)
_password = Value
End Set
End Property
Public Property csa() As String
Get
Return _csa
End Get
Set(ByVal Value As String)
_csa = Value
End Set
End Property
Public Property status() As String
Get
Return _status
End Get
Set(ByVal Value As String)
_status = Value
End Set
End Property
Public Sub New()
init()
End Sub
Private Sub init()
_userId = -1
_userName = ""
_password = ""
_csa = ""
_status = 1
End Sub
Public Function ValidateUser() As Boolean
Dim cmd As New SqlCommand("sp_validateUse
_conn.Open()
Dim rs As SqlDataReader
cmd.CommandType = Data.CommandType.StoredPro
cmd.Parameters.Add("@userN
cmd.Parameters.Add("@passw
rs = cmd.ExecuteReader()
If rs.Read() Then
'Make sure user is active
If (rs("status") <> 1) Then
Return False
End If
_userId = rs("userId")
_userName = rs("userName")
_csa = rs("csa")
_status = rs("status")
rs.Close()
_conn.Close()
Return True
Else
rs.Close()
_conn.Close()
Return False
End If
rs.Close()
_conn.Close()
End Function
Public Function LoadUser(ByVal userId As Integer)
Dim rs As SqlDataReader
Dim cmd As New SqlCommand("sp_selectUserD
cmd.CommandType = Data.CommandType.StoredPro
cmd.Parameters.Add("@userI
rs = cmd.ExecuteReader()
If rs.Read() Then
_userId = rs("userId")
_userName = rs("userName")
_csa = rs("csa")
_status = rs("status")
Return True
Else
Return False
End If
rs.Close()
End Function
Public Function DisplayUsers()
Dim rs As SqlDataReader
Dim cmd As New SqlCommand("sp_selectActiv
cmd.CommandType = Data.CommandType.StoredPro
rs = cmd.ExecuteReader()
While rs.Read()
_userId = rs("userId")
_userName = rs("userName")
_csa = rs("csa")
_status = rs("status")
End While
rs.Close()
Return Nothing
End Function
Public Function CountUsers(ByVal filterID As Integer)
Dim rs As SqlDataReader
Dim count As String = "0"
Dim thisFilter As Integer = CInt(filterID)
'set filter default
If thisFilter = 0 Then
thisFilter = 1
End If
'Open Database Connection
Dim conn As New SqlConnection(System.Confi
conn.Open()
Dim cmd As New SqlCommand("sp_Count_Users
cmd.CommandType = Data.CommandType.StoredPro
cmd.Parameters.Add("@UserS
rs = cmd.ExecuteReader()
If rs.Read() Then
'User Found
count = rs("getCount")
End If
conn.Close()
Return count
End Function
End Class
Here is how I am calling te class:
Dim user As New User
Dim conn As New SqlConnection(System.Confi
user.conn = conn
user.userName = username
user.password = password
If (user.ValidateUser() = True) Then
Response.Write("user passed")
Else
Response.Write("user failed")
End If
Thanks in advance
-Javier
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You were right, turned out I forgot to change the stored procedure to select the status field as well. I thought I had done that and forgot to double check.