Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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: There is no row at position 0.

Source Error:
Line 434:            System.Data.DataSet dt6 = new System.Data.DataSet();
Line 435:            da.Fill(dt6);
Line 436:            string amp5 = dt6.Tables[0].Rows[0][0].ToString();
Line 437:            string str6 = amp5.Replace("&", "&");
Line 438:            // ddlcomm.Text = str6;
Posted
Updated 10-Feb-13 19:15pm
v2

Apparently, it can happen when the number of rows is 0. You need to check for such cases before indexing a row.

—SA
 
Share this answer
 
the exception will be at line 436.

its always a good practice to check the table and row index before you operate on those variables. because at times you cannot guarantee it always returns the values as expected.
C#
if( dt6.Tables.Count >0 && dt6.Tables[0].Rows.Count > 0)
{  
 string amp5 = dt6.Tables[0].Rows[0][0].ToString();
}
 
Share this answer
 
Actually in this line you got this error message

C#
string amp5 = dt6.Tables[0].Rows[0][0].ToString();


In Row[0],there is no data that's way this error showing.
So, before that you check condition and then wrote this lines of code inside the If condition.

C#
if(dt6.Tables[0].Rows.Count>0)
{
    string amp5 = dt6.Tables[0].Rows[0][0].ToString();
}
 
Share this answer
 
Comments
Jibesh 11-Feb-13 0:37am    
what difference it makes from the above solution?
Naveen.Sanagasetti 11-Feb-13 4:48am    
sorry i'm not checking the previous solution.
It means, the table dt6 is not having any rows. It's a good practice to check for the null values firs before binding the values to the variables. Here is a sample I am writing. Try this and always check for the null values(Good Practice):
C#
System.Data.DataSet dt6 = new System.Data.DataSet();
da.Fill(dt6);
if(dt6!=null)
{
    if(dt6.Tables.Count > 0)
    {
        if(dt6.Tables[0].Rows.Count > 0)
        {
             if(dt6.Tables[0].Rows[0][0]!=DBNull.Value){
                  amp5 = dt6.Tables[0].Rows[0][0].ToString();
             }
        }
    }
}



--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900