Click here to Skip to main content
15,921,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used in grid view when allowpaging="true" and pagesize="5" but i click in insert row button it show error,

i used this code
C#
private void AddNewRow()
   {
       int rowIndex = 0;

       if (ViewState["CurrentTable"] != null)
       {
           DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
           DataRow drCurrentRow = null;
           if (dtCurrentTable.Rows.Count > 0)
           {
               for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
               {
                   TextBox txtEmpCode = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtEmpCode");                  TextBox txtHours = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("txtHours");
                   TextBox txtCode = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtCode");

                   drCurrentRow = dtCurrentTable.NewRow();
                   drCurrentRow["RowNumber"] = i + 1;

                   dtCurrentTable.Rows[i - 1]["empl_code"] = txtEmpCode.Text;
                   dtCurrentTable.Rows[i - 1]["hours"] = txtHours.Text;
                   dtCurrentTable.Rows[i - 1]["awr_code"] = txtCode.Text.ToUpper();
                   rowIndex++;
               }
               dtCurrentTable.Rows.Add(drCurrentRow);
               ViewState["CurrentTable"] = dtCurrentTable;

               grvStudentDetails.DataSource = dtCurrentTable;
               grvStudentDetails.DataBind();



           }
       }
       else
       {
           Response.Write("ViewState is null");
       }
       SetPreviousData();
   }

the error is:-"
SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

" this error come in bold line,

what when i am not using paging it will be correct.
what can i do please help me?
Posted
Updated 22-Mar-13 0:43am
v2
Comments
Raje_ 22-Mar-13 6:48am    
Snehal's solution seems good, or you can write (int i = 1; i < dtCurrentTable.Rows.Count; i++).
If it does not solve your issue then try putting breakpoint in your code and check which line of code throwing the exception.

1 solution

Error is because of this line
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
It should be
for (int i = 1; i <= dtCurrentTable.Rows.Count-1; i++)
 
Share this answer
 
Comments
rajan_gupta 22-Mar-13 6:28am    
no this is not a issue because i have dtCurrentTable.Rows.Count=26 but I use in pagesize=5, thats by whenever i=6 it show error, i mean i add the new row the after the 26 row but it cant read
rajan_gupta 22-Mar-13 6:29am    
please hepl me again
sunilgurav241 24-Jun-13 6:46am    
i have this problem .. so please help

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