ASP.NET - Grid View Command Issue - Asked By Tahir khan on 13-Jun-12 03:44 AM

i am using grid view have template fields and command i am adding insertcommand in grid to add new row first time if if i click on new second row is coming but if i click on second row click on new there is error message there is no row at position 1 .

Anyone have idea below is the please check it also i m using textbox for taking date from calendar extendar have to give the validation if one date is exist we can not enter another same date. existing date validation in needed help me if anyone have any idea...


protected void gvCalendar_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                DataTable dtcalander = null;
                dtcalander = CreateCalanderTable();  //(DataTable)ViewState["Calander"];
              
                int iRowIndex = 0;
                if (e.CommandName == "New")
                {
                    if (hdnFlag.Value ==__Add.ToString()) 
                    {
                        dtcalander.NewRow();
                        dtcalander.Rows.Add();
                       // iRowIndex = gvCalendar.PageIndex * gvCalendar.PageSize + int.Parse(e.CommandArgument.ToString());
                        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();
                        gvCalendar.DataSource = dtcalander;
                        gvCalendar.DataBind();
                    }
                    else if (hdnFlag.Value == __Edit.ToString())
                    {
                        dtcalander = (DataTable)ViewState["Calander"];
                        gvCalendar.DataSource = dtcalander;
                        gvCalendar.DataBind(); 
                    }


                }
                else if (e.CommandName == "Delete")
                {
                    iRowIndex = gvCalendar.PageIndex * gvCalendar.PageSize + int.Parse(e.CommandArgument.ToString());
                    dtcalander.Rows.RemoveAt(iRowIndex);
                    ViewState["Calander"] = dtcalander;
                    gvCalendar.DataSource = dtcalander;
                    gvCalendar.DataBind();             


                }
            }
            catch (Exception ex)
            {
                Session["Exception"] = ex;
                Response.Redirect("../Globals/ErrorPage.aspx");
             
            
            }
        }
[)ia6l0 iii replied to Tahir khan on 13-Jun-12 10:07 PM
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.
Jitendra Faye replied to Tahir khan on 14-Jun-12 01:30 AM
Change your code like this-

dtcalander.NewRow();

            
            // iRowIndex = gvCalendar.PageIndex * gvCalendar.PageSize + int.Parse(e.CommandArgument.ToString());
              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.Rows.Add();
              gvCalendar.DataSource = dtcalander;
              gvCalendar.DataBind();


Try this and let me know.

Tahir khan replied to Jitendra Faye on 14-Jun-12 03:37 AM
Thanks to replied i tried like that
while debugging on the highlited line i m getting error there is no row at position 0,

....... thanks   

 protected void gvCalendar_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                DataTable dtcalander = null;
                dtcalander = CreateCalanderTable();  //(DataTable)ViewState["Calander"];


                int iRowIndex = 0;
               
                if (e.CommandName == "New")
                {
                   // int iRowIndex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = gvCalendar.Rows[iRowIndex];
                    if (hdnFlag.Value ==__Add.ToString()) 
                    {
                       //dtcalander.NewRow();
                       //dtcalander.Rows.Add();
                      //  iRowIndex = Convert.ToInt32(e.CommandArgument);
                       // iRowIndex = gvCalendar.PageIndex  + int.Parse(e.CommandArgument.ToString());
                        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.Rows.Add();
                        gvCalendar.DataSource = dtcalander;
                        gvCalendar.DataBind();
                    }
                    else if (hdnFlag.Value == __Edit.ToString())
                    {
                        dtcalander = (DataTable)ViewState["Calander"];
                        gvCalendar.DataSource = dtcalander;
                        gvCalendar.DataBind(); 
                    }


                }
                else if (e.CommandName == "Delete")
                {
                    iRowIndex = gvCalendar.PageIndex * gvCalendar.PageSize + int.Parse(e.CommandArgument.ToString());
                    dtcalander.Rows.RemoveAt(iRowIndex);
                    ViewState["Calander"] = dtcalander;
                    gvCalendar.DataSource = dtcalander;
                    gvCalendar.DataBind();             


                }
            }
            catch (Exception ex)
            {
                Session["Exception"] = ex;
                Response.Redirect("../Globals/ErrorPage.aspx");
             
            
            }
        }