title: Index (zero based) must be greater than or equal to zero and less than the size of the argument list while inserting the arraylist
body: <p>I'm getting this error:</p>
  
  <blockquote>
    <p>Index (zero based) must be greater than or equal to zero and less than the size of the argument list. </p>
  </blockquote>
  
  <p>at here: </p>
  
  <pre><code>splitItems = item.Split(",".ToCharArray()); 
  
  sb.AppendFormat("{0}('{1}','{2}','{3}','{4}'); ",
        ds.InsertPlan("1", splitItems[0], splitItems[1], splitItems[2],
                      splitItems[3], DateTime.Now, DateTime.Now, user, user, "Nil", 1));
  </code></pre>
  
  <p>And the C# code is given below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Collections.Specialized;
  using System.Text;
  
  public partial class Site_Allocation_Plan_Entry : System.Web.UI.Page
  {
      private void SetInitialRowToGrid()
      {
          // Initialize and Set initial row of Datatable
          var tempDataTable = new DataTable();
          tempDataTable.Columns.Add("txtDate1");
          tempDataTable.Columns.Add("txtPlace1");
          tempDataTable.Columns.Add("txtPlace2");
          tempDataTable.Columns.Add("txtDistance");
          tempDataTable.Rows.Add("1", "", "", "");
  
          // Store that datatable into viewstate
          ViewState["TempTable"] = tempDataTable;
  
          // Attach Gridview Datasource to datatable
          GridView1.DataSource = tempDataTable;
          GridView1.DataBind();
      }
  
      private void AddNewRowToGrid()
      {
          int rowIndex = 0;
  
          if (ViewState["TempTable"] != null)
          {
              // Get TempTable from viewstate
              var tempTable = (DataTable)ViewState["TempTable"];
              DataRow tempRow = null;
  
              if (tempTable.Rows.Count &gt; 0)
              {
                  for (int i = 1; i &lt;= tempTable.Rows.Count; i++)
                  {
                      // Get Grid's TextBox values
                      var dateText =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtDate1");
                      var place1Text =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtPlace1");
                      var place2Text =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtPlace2");
                      var distanceText =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtDistance");
  
                      // Create new row and update Row Number
                      tempRow = tempTable.NewRow();
  
                      tempTable.Rows[i - 1]["txtDate1"] = dateText.Text;
                      tempTable.Rows[i - 1]["txtPlace1"] = place1Text.Text;
                      tempTable.Rows[i - 1]["txtPlace2"] = place2Text.Text;
                      tempTable.Rows[i - 1]["txtDistance"] = distanceText.Text;
  
                      rowIndex++;
                  }
  
                  // Add data to datatable and viewstate
                  tempTable.Rows.Add(tempRow);
                  ViewState["TempTable"] = tempTable;
  
                  // Attach Gridview Datasource to datatable
                  GridView1.DataSource = tempTable;
                  GridView1.DataBind();
              }
          }
  
          //Set Previous Data on Postbacks
          SetPreviousData();
      }
  
      private void SetPreviousData()
      {
          int rowIndex = 0;
  
          if (ViewState["TempTable"] != null)
          {
              var tempTable = (DataTable)ViewState["TempTable"];
  
              if (tempTable.Rows.Count &gt; 0)
              {
                  for (int i = 0; i &lt; tempTable.Rows.Count; i++)
                  {
                      var dateText =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtDate1");
                      var place1Text =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtPlace1");
                      var place2Text =
                          (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtPlace2");
                      var distanceText =
                          (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtDistance");
  
                      dateText.Text = tempTable.Rows[i]["txtDate1"].ToString();
                      place1Text.Text = tempTable.Rows[i]["txtPlace1"].ToString();
                      place2Text.Text = tempTable.Rows[i]["txtPlace2"].ToString();
                      distanceText.Text = tempTable.Rows[i]["txtDistance"].ToString();
  
                      rowIndex++;
                  }
              }
          }
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              this.SetInitialRowToGrid();
          }
      }
  
      protected void ButtonAdd_Click(object sender, EventArgs e)
      {
          AddNewRowToGrid();
      }
  
      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
  
          int index = Convert.ToInt32(e.RowIndex);
  
          deleteRow(index);
          SetPreviousData();
      }
      private int deleteRow(int index)
      {
          int rowIndex = 0;
  
          if (ViewState["TempTable"] != null)
          {
              // Get TempTable from viewstate
              var tempTable = (DataTable)ViewState["TempTable"];
  
  
              if (tempTable.Rows.Count &gt; 0)
              {
                  for (int i = 1; i &lt;= tempTable.Rows.Count; i++)
                  {
                      // Get Grid's TextBox values
                      var dateText =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtDate1");
                      var place1Text =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtPlace1");
                      var place2Text =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtPlace2");
                      var distanceText =
                          (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtDistance");                    
                  }
  
                  // Add data to datatable and viewstate                
                  tempTable.Rows.RemoveAt(index);
                  ViewState["TempTable"] = tempTable;
  
                  // Attach Gridview Datasource to datatable
                  GridView1.DataSource = tempTable;
                  GridView1.DataBind();
              }
          }
  
          //Set Previous Data on Postbacks        
          return index;
      }
  
      protected void btnSave_Click(object sender, EventArgs e)
      {
          int rowIndex = 0;
          StringCollection sc = new StringCollection();
          if (ViewState["TempTable"] != null)
          {
              DataTable dtCurrentTable = (DataTable)ViewState["TempTable"];
              if (dtCurrentTable.Rows.Count &gt; 0)
              {
                  for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
                  {
                      //extract the TextBox values
                      TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtPlace1");
                      TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtPlace2");
                      TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtDistance");
                      TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("txtDate1");
  
                      //get the values from the TextBoxes
                      //then add it to the collections with a comma "," as the delimited values
                      sc.Add(box1.Text + "," + box2.Text + "," + box3.Text + "," + box4.Text);
                      rowIndex++;
                  }
                  //Call the method for executing inserts
                  InsertRecords(sc);
              }
          }
      }
      private void InsertRecords(StringCollection sc)
      {
          DS_SiteDataTableAdapters.tbl_planTableAdapter ds;
          ds = new DS_SiteDataTableAdapters.tbl_planTableAdapter();
  
          StringBuilder sb = new StringBuilder(string.Empty);
          string[] splitItems = null;
          string user = Page.User.Identity.Name;
          foreach (string item in sc)
          {
  
  
              if (item.Contains(","))
              {
                  splitItems = item.Split(",".ToCharArray());
                  sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}'); ", ds.InsertPlan("1", splitItems[0], splitItems[1], splitItems[2], splitItems[3], DateTime.Now, DateTime.Now, user, user, "Nil", 1));
              }
  
          }
  
          try
          {
              //Display a popup which indicates that the record was successfully inserted
              Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
  
          }
          catch (System.Data.SqlClient.SqlException ex)
          {
              string msg = "Insert Error:";
              msg += ex.Message;
              throw new Exception(msg);
  
          }       
      }
  }
  </code></pre>
  
  <p>Any help will be greatly appreciated...</p>