Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Exception: Array index out of bounds

Exception: Array index out of bounds

Scheduled Pinned Locked Moved C#
csharpdatabasedata-structuresquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kranti1251984
    wrote on last edited by
    #1

    Hi, I'm writing an application in c#.net and i'm completely new to it. Please refer to the following code in which i'm getting an exception... //..................................................................... String classStartTime; conn.Open(); String myQuery = "Select starttime from tblecture"; OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(myQuery, conn); OdbcDataReader dataReader = myCommand.ExecuteReader(); try { while (dataReader.Read()) //exception classStartTime = dataReader.GetTime(0).ToString(); } finally { dataReader.Close(); conn.Close(); } //..................................................................... I'm getting this exception for only column named "starttime"; for other fields this code works fine. Is there any other way to read a field of type time from database? Thanks, Kranti -- modified at 4:13 Wednesday 19th April, 2006

    T G 2 Replies Last reply
    0
    • K Kranti1251984

      Hi, I'm writing an application in c#.net and i'm completely new to it. Please refer to the following code in which i'm getting an exception... //..................................................................... String classStartTime; conn.Open(); String myQuery = "Select starttime from tblecture"; OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(myQuery, conn); OdbcDataReader dataReader = myCommand.ExecuteReader(); try { while (dataReader.Read()) //exception classStartTime = dataReader.GetTime(0).ToString(); } finally { dataReader.Close(); conn.Close(); } //..................................................................... I'm getting this exception for only column named "starttime"; for other fields this code works fine. Is there any other way to read a field of type time from database? Thanks, Kranti -- modified at 4:13 Wednesday 19th April, 2006

      T Offline
      T Offline
      Tehnoon
      wrote on last edited by
      #2

      There is a very obvious mistake that you are making here. The query myQuery is returning you only one column per row. You are using dataReader.GetTime(1) , which is trying to get the second column (indexes are zero based, not 1 based) and hence generating an index out of range exception, because there is nothing at the "1" ordinal. Solution: Change the line classStartTime = dataReader.GetTime(1).ToString(); to classStartTime = dataReader.GetTime(0).ToString();

      J 1 Reply Last reply
      0
      • K Kranti1251984

        Hi, I'm writing an application in c#.net and i'm completely new to it. Please refer to the following code in which i'm getting an exception... //..................................................................... String classStartTime; conn.Open(); String myQuery = "Select starttime from tblecture"; OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(myQuery, conn); OdbcDataReader dataReader = myCommand.ExecuteReader(); try { while (dataReader.Read()) //exception classStartTime = dataReader.GetTime(0).ToString(); } finally { dataReader.Close(); conn.Close(); } //..................................................................... I'm getting this exception for only column named "starttime"; for other fields this code works fine. Is there any other way to read a field of type time from database? Thanks, Kranti -- modified at 4:13 Wednesday 19th April, 2006

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        The first field has index 0, not 1. --- b { font-weight: normal; }

        K 1 Reply Last reply
        0
        • T Tehnoon

          There is a very obvious mistake that you are making here. The query myQuery is returning you only one column per row. You are using dataReader.GetTime(1) , which is trying to get the second column (indexes are zero based, not 1 based) and hence generating an index out of range exception, because there is nothing at the "1" ordinal. Solution: Change the line classStartTime = dataReader.GetTime(1).ToString(); to classStartTime = dataReader.GetTime(0).ToString();

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          In addition to the above, if your variable "classStartTime" is of type DateTime (as it should be) then you do not need the .ToString() call - it will cause an invalid cast exception. if classStartTime is a string: classStartTime = dataReader.GetString(0); if classStartTime is DateTime: classStartTime = dataReader.GetTime(0);

          1 Reply Last reply
          0
          • G Guffa

            The first field has index 0, not 1. --- b { font-weight: normal; }

            K Offline
            K Offline
            Kranti1251984
            wrote on last edited by
            #5

            even when i make it as ... //................................................... while (dataReader.Read()) //exception classStartTime = dataReader.GetTime(0).ToString(); //................................................... it generates the exception at the previous line i.e. the line containing "while", before going for this statement Thanks, Kranti

            J 1 Reply Last reply
            0
            • K Kranti1251984

              even when i make it as ... //................................................... while (dataReader.Read()) //exception classStartTime = dataReader.GetTime(0).ToString(); //................................................... it generates the exception at the previous line i.e. the line containing "while", before going for this statement Thanks, Kranti

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              What is the exception generated?

              K 1 Reply Last reply
              0
              • J J4amieC

                What is the exception generated?

                K Offline
                K Offline
                Kranti1251984
                wrote on last edited by
                #7

                the exception generated is "Array index out of bounds"

                J 1 Reply Last reply
                0
                • K Kranti1251984

                  the exception generated is "Array index out of bounds"

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  Array Index out of bounds was when you were trying to read element 1, that exception is unlikely to be being generated by the call to while(reader.Read()). try posting the complete code, that is the relevant bit that is opening connection, creating command etc.

                  K 1 Reply Last reply
                  0
                  • J J4amieC

                    Array Index out of bounds was when you were trying to read element 1, that exception is unlikely to be being generated by the call to while(reader.Read()). try posting the complete code, that is the relevant bit that is opening connection, creating command etc.

                    K Offline
                    K Offline
                    Kranti1251984
                    wrote on last edited by
                    #9

                    the complete code is as follows ... //.................................. String connectionString = "Dsn=pgTEducation;database=teducation;server=192.168.0.135;port=5432;uid=divinet;readonly=0;protocol=6.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;socket=4096;unknownsizes=0;maxvarcharsize=254;maxlongvarcharsize=8190;debug=0;commlog=0;optimizer=1;ksqo=1;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;cancelasfreestmt=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;disallowpremature=0;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=0"; OdbcConnection conn = new System.Data.Odbc.OdbcConnection(connectionString); String classStartTime; conn.Open(); String myQuery = "Select starttime from tblecture"; OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(myQuery, conn); OdbcDataReader dataReader = myCommand.ExecuteReader(); try { while (dataReader.Read()) classStartTime = dataReader.GetTime(0).ToString(); } finally { dataReader.Close(); conn.Close(); } //.................................. thanks, Kranti

                    J 1 Reply Last reply
                    0
                    • K Kranti1251984

                      the complete code is as follows ... //.................................. String connectionString = "Dsn=pgTEducation;database=teducation;server=192.168.0.135;port=5432;uid=divinet;readonly=0;protocol=6.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;socket=4096;unknownsizes=0;maxvarcharsize=254;maxlongvarcharsize=8190;debug=0;commlog=0;optimizer=1;ksqo=1;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;cancelasfreestmt=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;disallowpremature=0;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=0"; OdbcConnection conn = new System.Data.Odbc.OdbcConnection(connectionString); String classStartTime; conn.Open(); String myQuery = "Select starttime from tblecture"; OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(myQuery, conn); OdbcDataReader dataReader = myCommand.ExecuteReader(); try { while (dataReader.Read()) classStartTime = dataReader.GetTime(0).ToString(); } finally { dataReader.Close(); conn.Close(); } //.................................. thanks, Kranti

                      J Offline
                      J Offline
                      J4amieC
                      wrote on last edited by
                      #10

                      The problem may be to do with your use of GetTime(int). The documentation of this states "Gets the value of the specified column as a System.TimeSpan object". Im thinking what you actually want is GetDateTime(int). However, as with my post above, if all you need is a string, then use GetString(int)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups