Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Getting Run time exception within Form_Load line number 11

C#
private void frmEmpBonusList_Load(object sender, EventArgs e)
       {
           dgv_EmpBonusList.AutoGenerateColumns = false;
           da = new SqlDataAdapter("Select docno,docdate,BonusName,BonusType,minSalary,maxSalary,PerAmount from EmpBonus order by docno,minsalary", constring);
           try
           {
               ds = new DataSet();
               da.Fill(ds);
               dgv_EmpBonusList.DataSource = ds.Tables[0];
               for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
               {

                   dgv_EmpBonusList.ReadOnly = true;
                   dgv_EmpBonusList.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i][0].ToString();
                   dgv_EmpBonusList.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i][1].ToString();
                   dgv_EmpBonusList.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i][2].ToString();
                   dgv_EmpBonusList.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i][3].ToString();
                   dgv_EmpBonusList.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i][4].ToString();
                   dgv_EmpBonusList.Rows[i].Cells[5].Value = ds.Tables[0].Rows[i][5].ToString();
                   dgv_EmpBonusList.Rows[i].Cells[6].Value = ds.Tables[0].Rows[i][6].ToString();
                   cmgr = (CurrencyManager)dgv_EmpBonusList.BindingContext[ds.Tables[0]];
                   this.dgv_EmpBonusList.EnableHeadersVisualStyles = false;
                   this.dgv_EmpBonusList.ColumnHeadersHeight = 20;
                   this.dgv_EmpBonusList.BackgroundColor = Color.LightBlue;
               }

           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               da.Dispose();
           }
       }
Posted
Comments
hypermellow 28-Apr-15 6:39am    
Where in your code are you getting the exception? ... what line is line number 11?
Meer Wajeed Ali 28-Apr-15 6:41am    
Sorry that is line no 13.
hypermellow 28-Apr-15 6:42am    
Is line number 13: dgv_EmpBonusList.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i][1].ToString();
Meer Wajeed Ali 28-Apr-15 6:48am    
Yes. after compiling this line jumping out of try block

Hi,


If your exception is getting raised on line 13, then it looks like either:
1) your datatable does not contain the number of columns you are expecting. i.e. - your datatable does not contain more that 1 column.

2) Your DataGridView control does not contain the number of columns youare expecting.

I've amended your code to add a couple of basic checks, that will avoid this type of exceptions being thrown.

The 1st one is:
C#
if (ds.Tables.Count > 0)

... this checks you have a datatable.

The 2nd one is:
C#
if (ds.Tables[0].Columns.Count > 6)

... this checks your datatable contains the amount of columns you are expecting.

The 3rd one is:
C#
if(dgv_EmpBonusList.ColumnCount>6)

... this checks your datagridview control contained the amount of columns you are expecting.

This will not resolve your issue of the number of columns being less that expected, but it will avoid the exception begin thrown.

C#
private void frmEmpBonusList_Load(object sender, EventArgs e)
{
    dgv_EmpBonusList.AutoGenerateColumns = false;
    da = new SqlDataAdapter("Select docno,docdate,BonusName,BonusType,minSalary,maxSalary,PerAmount from EmpBonus order by docno,minsalary", constring);
    try
    {
        ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables.Count > 0)
        {
            dgv_EmpBonusList.DataSource = ds.Tables[0];
            if ((ds.Tables[0].Columns.Count > 6)&&(dgv_EmpBonusList.ColumnCount>6))
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    dgv_EmpBonusList.ReadOnly = true;
                    dgv_EmpBonusList.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i][0].ToString();
                    dgv_EmpBonusList.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i][1].ToString();
                    dgv_EmpBonusList.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i][2].ToString();
                    dgv_EmpBonusList.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i][3].ToString();
                    dgv_EmpBonusList.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i][4].ToString();
                    dgv_EmpBonusList.Rows[i].Cells[5].Value = ds.Tables[0].Rows[i][5].ToString();
                    dgv_EmpBonusList.Rows[i].Cells[6].Value = ds.Tables[0].Rows[i][6].ToString();
                    cmgr = (CurrencyManager)dgv_EmpBonusList.BindingContext[ds.Tables[0]];
                    this.dgv_EmpBonusList.EnableHeadersVisualStyles = false;
                    this.dgv_EmpBonusList.ColumnHeadersHeight = 20;
                    this.dgv_EmpBonusList.BackgroundColor = Color.LightBlue;
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        da.Dispose();
    }
}


... hope it helps.
 
Share this answer
 
v3
Comments
Sascha Lefèvre 28-Apr-15 7:05am    
I think the DataTable has to contain exactly 7 columns because that's how many the query selects - otherwise the query execution should throw an exception. But I have no other idea.. :)
hypermellow 28-Apr-15 7:11am    
Yes, it does look like the DataTable should contain 7 columns, the only other possible unknown looks to be DataGridViews column count.

... I've now updated to add a check for this.
Sascha Lefèvre 28-Apr-15 7:15am    
I think you nailed it there. He sets AutoGenerateColumns=false at the top and doesn't create columns manually.

+5
hypermellow 28-Apr-15 7:21am    
Very good spot, ... the AutoGenerateColumns=false; does say it all :-) - Many Thanks
You must Change the For-column to this :
for (int i = 0; i < ds.Tables[0].Rows.Count -1; i++)


A collection is Zero-based (as you recognized) but the last item in the collection is Count -1 ...
 
Share this answer
 
Comments
Meer Wajeed Ali 28-Apr-15 6:32am    
still getting same exception..
Ralf Meier 28-Apr-15 6:52am    
are you sure that ds.tables is not NULL (or NOTHING) ?
Meer Wajeed Ali 28-Apr-15 6:54am    
When I am filling I,e da.Fill(ds); I checked there are data into dataset object
Ralf Meier 28-Apr-15 7:01am    
Please set a Breakpoint before the for-line and look what this element says : dgv_EmpBonusList.DataSource
Richard Deeming 28-Apr-15 8:12am    
The loop in the original question is correct. Your loop will always skip the last item in the collection.

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