Exception: Array index out of bounds
-
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
-
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
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();
-
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
-
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();
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);
-
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
-
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
-
the exception generated is "Array index out of bounds"
-
the exception generated is "Array index out of bounds"
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.
-
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.
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
-
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
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 isGetDateTime(int)
. However, as with my post above, if all you need is a string, then useGetString(int)