You should debug the code, and find the row that throws this error. At a short glance, I see that all the code under this can throw the error.
dtcalander.NewRow();
dtcalander.Rows.Add();
dtcalander.Rows[iRowIndex]["Year"] = ((DropDownList)gvCalendar.Rows[iRowIndex].FindControl("ddlYear")).Text.Trim();
dtcalander.Rows[iRowIndex]["Date"] = ((TextBox)gvCalendar.Rows[iRowIndex].FindControl("txtCalc")).Text.Trim();
dtcalander.Rows[iRowIndex]["Reason"] = ((TextBox)gvCalendar.Rows[iRowIndex].FindControl("txtReason")).Text.Trim();
dtcalander.Rows[iRowIndex]["status"] = ((DropDownList)gvCalendar.Rows[iRowIndex].FindControl("ddlStatus")).Text.Trim();
ViewState["Calander"] = dtcalander;
dtcalander.NewRow();
dtcalander.Rows.Add();
This is what is normally done.
private void gridviewtemporaray_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Insert")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gridviewtemporaray.Rows[index];
//get the values
Label label1 = (Label)row.FindControl("labelnamel");
//All other code follows
}
}
And why would you add a newrow before and after you get the data from the code? I have highlighted them above. Please fix that as well.
Hope this helps.