Solved

error: System.IndexOutOfRangeException:

Posted on 2009-05-07
14
379 Views
Last Modified: 2013-12-17
Hello,

I'm getting this error when I try to read the data from the db

I'm trying to read:
CDI_N varchar 50
CDI_D varchar 50
CDI_C float  8
CDI_NA bit 1
the problem is on the CDI_C

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: CDI_C

thanks
private void Get_Data()
    {
        Reset_Data();
 
        SqlCommand cmd = new SqlCommand("sp_GetlData", myConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        cmd.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        cmd.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
        SqlDataReader dr = null;
        myConnection.Open();
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
  //  CDI
 
            string xCDIN = Convert.ToString(dr["CDI_N"].ToString());
            if (xVAPN.Length != 0)
            {
                CDI_N = Convert.ToDouble(xCDIN);
                TextBox9.Text = xCDIN;
            }
 
            string xCDID = Convert.ToString(dr["CDI_D"].ToString());
            if (xCDID.Length != 0)
            {
                CDI_D = Convert.ToDouble(xCDID);
                TextBox10.Text = xCDID;
            }
 
            read CDI_C from DB as an int
            if (dr["CDI_C"] != null)
            {
                CDI_C = Convert.ToInt32(dr["CDI_C"]);
            }
            else CDI_C = 0;
 
            sCheckbox = dr["CDI_NA"].ToString();
            if (Convert.ToBoolean(dr["CDI_NA"]))
            {
                CheckBox5.Checked = true;
                TextBox9.Text = "";
                TextBox10.Text = "";
            }
        }
        if (dr != null)
            dr.Close();
        myConnection.Close();
    }
 private void Get_PrevMonth()
    {
        SqlCommand cmd3 = new SqlCommand("sp_GetData", myConnection);
        cmd3.CommandType = CommandType.StoredProcedure;
        cmd3.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        cmd3.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        cmd3.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
        SqlDataReader dr2 = null;
        myConnection.Open();
        dr2 = cmd3.ExecuteReader();
        if (dr2.Read())
        {
            ///read CDI_C from DB as an int
            if (dr2["CDI_C"] != null)
                CDI_C = Convert.ToDouble(dr2["CDI_C"]);
            else CDI_C = 0;
        }
        if (dr2 != null)
            dr2.Close();
        myConnection.Close();

Open in new window

0
Comment
Question by:Romacali
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24328787
you are not returning the field "CDI_C" in your stored procedure "sp_GetlData"


Ghost
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24328802
or maybe the sp_GetData stored procedure..

Exactly what line is it failing on...you find that...you'll find the sp with the problem,
0
 
LVL 13

Expert Comment

by:iHadi
ID: 24328809
Hi

At what line is the exception occurring, and can you post the exceptions stack trace.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Romacali
ID: 24328865
I added Debug="true" and the error points to: Line 256:            if (dr["CDI_C"] != null)

In the database the CDI_N, CDI_C, CDI_NA are value =  NULL

 CDI_C
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: CDI_C

Source Error:

Line 254:
Line 255:            //read CDI_C from DB as an int
Line 256:            if (dr["CDI_C"] != null)
Line 257:            {
Line 258:                CDI_C = Convert.ToInt32(dr["CDI_C"]);
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24328886
you can't convert null to int.
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24328895
oops never mind that...(didn't catch the if :)

Make sure you are returning that value in your stored procedure...
0
 

Author Comment

by:Romacali
ID: 24328898
here is the getdata procedure
ALTER   PROCEDURE dbo.sp_GetData(@Login as varchar(25), @DataYr as int, @DataQtr as tinyint)
AS
    BEGIN
        declare @oshpid int
        
       set @oshpid  = (select OSHPDID
	   from _Login 
	   where UserID = @Login)
	   
	   select CDI_N, CDI_D, CDI_NA
	   from _Data
	   where OSHPDID = @oshpid
	   and DataYr = @DataYr
	   and DataQtr = @DataQtr
    END

Open in new window

0
 
LVL 13

Assisted Solution

by:copyPasteGhost
copyPasteGhost earned 200 total points
ID: 24328907
select CDI_N, CDI_D, CDI_NA CDI_C
         from _Data
         where OSHPDID = @oshpid
         and DataYr = @DataYr
         and DataQtr = @DataQtr

There is your problem.
0
 

Author Comment

by:Romacali
ID: 24328963
humm I was missing the CDI_C from the storedp.. the problem now is:

pointing to: Line 258:                CDI_C = Convert.ToInt32(dr["CDI_C"]);

Server Error in '/' Application.
Object cannot be cast from DBNull to other types.
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: Object cannot be cast from DBNull to other types.

Source Error:

Line 256:            if (dr["CDI_C"] != null)
Line 257:            {
Line 258:                CDI_C = Convert.ToInt32(dr["CDI_C"]);
Line 259:            }
Line 260:            else CDI_C = 0;
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24328982
CDI_C = dr["CDI_C"] == DBNull ? 0 : Convert.ToInt32(dr["CDI_C"]);
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24328990
Note the "0" between the ? and the  : can be any default value you would like for CDI_C.
0
 

Author Comment

by:Romacali
ID: 24329056
I got this now:
Compiler Error Message: CS0118: 'System.DBNull' is a 'type' but is used like a 'variable'

Source Error:

Line 256:            if (dr["CDI_C"] != null)
Line 257:            {
Line 258:                CDI_C = dr["CDI_C"] == DBNull ? 0 : Convert.ToInt32(dr["CDI_C"]);

   if (dr["CDI_C"] != null)
            {
                CDI_C = dr["CDI_C"] == DBNull ? 0 : Convert.ToInt32(dr["CDI_C"]);
            }
            else CDI_C = 0;

Open in new window

0
 
LVL 13

Accepted Solution

by:
iHadi earned 300 total points
ID: 24329466
try:
if (!dr.IsDBNull(dr.GetOrdinal("CDI_C")))
{
    CDI_C = Convert.ToInt32(dr["CDI_C"]);
}

Open in new window

0
 

Author Comment

by:Romacali
ID: 24329913
that worked now I'm getting:
 Input string was not in a correct format.
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.FormatException: Input string was not in a correct format.

Line 433:            //CDI from previous yr
Line 434:            pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());
Line 435:            if (Convert.ToString(dr["CDI_C"].ToString()) == "")
Line 436:            {
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
array not updating 8 40
Validation for Passport expiry in asp.net 2 31
Put File 6 34
Why am I losing my session variable value 11 34
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question