Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 10th, 2004, 01:40 AM
Authorized User
 
Join Date: May 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default System.Index out of Bounds

This is the code in my .aspx file

Code:
sub verifyLogin (Source as Object, e as EventArgs)
 
    Dim objCon As New OleDBConnection(connectionString)
    Dim objCon1 as new OleDbConnection(connectionString) 

    Dim objCommand as OleDbCommand
    Dim objCommand1 as OleDbCommand

    Dim objDataReader as OleDBDataReader
    Dim objDataReader1 as OleDbDataReader 

    Dim StrSQL, StrSQL1, strSQL2 as String 

    Dim dbcomm, dbread

    dim lastlogins as datetime

    strSQL = "select emp_no from Employee; select course_id from course; " _
            & "select emp_no from employee where status = 'active' or status = 'inactivated'"
    objCon.Open() 
    objCommand = new OLEDBCommand (strSQL, objCon) 
    objDataReader = objCommand.ExecuteReader(CommandBehavior.Closeconnection) 

    Dim validUser 
    validUser = false

    While objDataReader.Read() 
        if txtEmpID.Text = objDataReader.GetString(0) then
        if txtEmpID.Text = objDataReader.GetString(2) then
            Session("userSession") = objDataReader.GetString(2)

    strSQL2 = "select max(login_time) from emp_login where emp_no = " _
            & "'"& Session("userSession") &"'"

        objCon1.open()

        dbcomm = new OleDbCommand(strSQL2, objCon1) 
        dbread = dbcomm.executeReader()

        dbread.read()
        Session("lastlogin") = dbread.getvalue(0)

        if ((Session("lastlogin"))is DBNull.Value ) then 
        Session("lastloginDate") = ""
        else 
        lastlogins = convert.toDateTime(Session("lastlogin"))
        Session("lastloginDate") = lastlogins.toString("d")
        end if 

        dbread.close()
        objCon1.close()

            Session("loginTime") = datetime.now().toString("T")
            Session("loginDate") = datetime.now().toString("d") 

            Session("loginData") = datetime.now().toString()

            Session("course_id") = objDataReader.GetString(1)

    strSQL1 = "insert into Emp_Login (Emp_No, Login_time, logout_time) " _
            & "values ('"& Session("userSession") &"', '"& Session("loginData") &"', '')"

        objCon1.open()
        objCommand1 = new OLEDBCommand (strSQL1, objCon1) 
        objDataReader1 = objCommand1.ExecuteReader(CommandBehavior.Closeconnection) 

        objDataReader1.Close()
        objCon1.Close()

        Server.transfer("EHomePage.aspx")
        exit while 

    else  
        if txtEmpID.Text = objDataReader.GetString(0) then 
            errorTxt.text = "Your status is inactive! Please contact system administrator" 
            txtEmpId.Text= ""

        else 
            errorTxt.text = "Invalid Login! Please try again or contact system administrator" 
            txtEmpId.Text= ""
        end if 

    end if 
    end if 

    end while 

objDataReader.Close()
objCon.Close()
end sub
I get the following error
Code:
Index was outside the bounds of the array. 
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.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error: 


Line 52:     While objDataReader.Read() 
Line 53:         if txtEmpID.Text = objDataReader.GetString(0) then
Line 54:         if txtEmpID.Text = objDataReader.GetString(2) thenLine 55:             Session("userSession") = objDataReader.GetString(2)
Line 56:             
 

Source File: c:\inetpub\wwwroot\ELearning\ELogin.aspx    Line: 54 

Stack Trace: 


[IndexOutOfRangeException: Index was outside the bounds of the array.]
   System.Data.OleDb.OleDbDataReader.DoValueCheck(Int32 ordinal)
   System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal)
   ASP.ELogin_aspx.verifyLogin(Object Source, EventArgs e) in c:\inetpub\wwwroot\ELearning\ELogin.aspx:54
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
Any input would be appreciated.



 
Old June 10th, 2004, 04:35 AM
Authorized User
 
Join Date: May 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem I have narrowed down to is - I want the following code
Code:
 if txtEmpID.Text = objDataReader.GetString(2) then
to use the 3rd SQL statement.
If you see my SQL variable it has three statements.
Code:
strSQL = "select emp_no from Employee; select course_id from course; " _
            & "select emp_no from employee where status = 'active' or status = 'inactivated'"
            So how do I call each one individually as you can see what I am trying to do.

Many Thanks.

 
Old June 10th, 2004, 07:34 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When you "Read()" from a DataReader, you are asking for the next row in the current result set. Then you use GetString(3) which is asking for the 3rd COLUMN in the current row.
You are generating 3 result sets with that query. Each result set has a single returned column.

What you are looking for is the NextResult() Method. From the MSDN docs: "Advances the data reader to the next result, when reading the results of batch Transact-SQL statements."

You need to:
- Execute the query
- Check/get first row (objDataReader.Read())
- Get the first column of first result set (objDataReader.GetString(0))
- Advance result set (objDataReader.NextResult())
- Check/get first row (objDataReader.Read())
- Get the first column of second result set (objDataReader.GetString(0))
- Advance result set (objDataReader.Read())
- Check/get first row (objDataReader.NextResult())
- Get the first column of third result set (objDataReader.GetString(0))
 
Old June 10th, 2004, 12:39 PM
Authorized User
 
Join Date: May 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. I did what you said.

Code:
sub verifyLogin (Source as Object, e as EventArgs)
 
    Dim objCon As New OleDBConnection(connectionString)
    Dim objCon1 as new OleDbConnection(connectionString) 

    Dim objCommand as OleDbCommand
    Dim objCommand1 as OleDbCommand

    Dim objDataReader as OleDBDataReader
    Dim objDataReader1 as OleDbDataReader 

    Dim StrSQL, StrSQL1, strSQL2 as String 

    Dim dbcomm, dbread

    dim lastlogins as datetime

    strSQL = "select emp_no from Employee where status = 'active' or status = 'inactivated' " _
            & "; select course_id, course_name from course; select emp_no from Employee;"

    objCon.Open() 
    objCommand = new OLEDBCommand (strSQL, objCon) 
    objDataReader = objCommand.ExecuteReader()

    Dim validUser 
    validUser = false

    While objDataReader.Read() 
        if txtEmpID.Text = objDataReader.GetString(0) then
            validUser = true 
            Session("userSession") = objDataReader.Getvalue(0).toString()

    strSQL2 = "select max(login_time) from emp_login where emp_no = " _
            & "'"& Session("userSession") &"'"

        objCon1.open()


        dbcomm = new OleDbCommand(strSQL2, objCon1) 
        dbread = dbcomm.executeReader()

        dbread.read()
        Session("lastlogin") = dbread.getvalue(0)

        if ((Session("lastlogin"))is DBNull.Value ) then 
        Session("lastloginDate") = ""
        else 
        lastlogins = convert.toDateTime(Session("lastlogin"))
        Session("lastloginDate") = lastlogins.toString("d")
        end if 

        dbread.close()
        objCon1.close()

            Session("loginTime") = datetime.now().toString("T")
            Session("loginDate") = datetime.now().toString("d") 

            Session("loginData") = datetime.now().toString()

        objDataReader.NextResult()
        while objDataReader.Read() 
         if portalList.SelectedItem.value = objDataReader.GetString(1) then
            Session("course_id") = objDataReader.GetString(0)
        end if 
        end while 

    strSQL1 = "insert into Emp_Login (Emp_No, Login_time, logout_time) " _
            & "values ('"& Session("userSession") &"', '"& Session("loginData") &"', '')"

        objCon1.open()
        objCommand1 = new OLEDBCommand (strSQL1, objCon1) 
        objDataReader1 = objCommand1.ExecuteReader(CommandBehavior.Closeconnection) 

        objDataReader1.Close()
        objCon1.Close()

        Server.transfer("EHomePage.aspx")

        end if 

        if not validUser then 
            objDataReader.NextResult()
            while objDataReader.Read()

            if txtEmpID.Text = objDataReader.GetString(0) then 
                errorTxt.Text = "Your status is inactive! Please contact system administrator" 
                txtEmpId.Text= ""
            else 
                errorTxt.text = "Invalid Login! Please try again or contact system administrator" 
                txtEmpId.Text= ""
            end if  
            end while 
        end if 

    end while 

objDataReader.Close()
objCon.Close()
end sub
Now I get this error

Server Error in '/ELearning' Application.
--------------------------------------------------------------------------------

The data value could not be converted for reasons other than sign mismatch or data overflow. For example, the data was corrupted in the data store but the row was still retrievable.
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.InvalidCastException: The data value could not be converted for reasons other than sign mismatch or data overflow. For example, the data was corrupted in the data store but the row was still retrievable.

Source Error:


Line 108: while objDataReader.Read()
Line 109:
Line 110: if txtEmpID.Text = objDataReader.GetString(0) then Line 111: errorTxt.Text = "Your status is inactive! Please contact system administrator"
Line 112: txtEmpId.Text= ""


Source File: c:\inetpub\wwwroot\ELearning\ELogin.aspx Line: 110

Stack Trace:


[InvalidCastException: The data value could not be converted for reasons other than sign mismatch or data overflow. For example, the data was corrupted in the data store but the row was still retrievable.]
   System.Data.OleDb.DBBindings.get_ValueString()
   System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal)
   ASP.ELogin_aspx.verifyLogin(Object Source, EventArgs e) in c:\inetpub\wwwroot\ELearning\ELogin.aspx:110
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
   System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

I also tried to make the .getString(0) to .getValue(0).toString()
but then I get the wrong error message. I have two error messages. I am supposed to get the first one (see above code) if there is a value in the database but instead I am getting the second one.

Please inform me if I there are any doubts. Your help is greatly appreciated.










Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was outside the bounds of the array ECPcenter ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2008 04:55 AM
Chapter 4 - SQL Error "Out of Bounds" scottt40 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 April 7th, 2007 10:56 AM
Array Bounds mjhoagland Classic ASP Basics 9 September 28th, 2006 09:01 AM
Index was outside the bounds of the array dba123 Reporting Services 0 March 6th, 2006 11:25 AM
index outside bounds of array error? barmanvarn BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 February 17th, 2005 03:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.