Saving data from temporary table to database
<p>I have been trying to save data from two Gridviews into a single table on a single button click.
Scenario:</p>

<p>Both  gridviews have a template checkbox field. Upon selecting one row from the first grid and another row from the second grid (which describes the offers that apply to the contents in the first grid) the data will be temporarily stored in a datatable and when the iteration completes for the checked checkboxes in both the grids, the data is saved in the DB.</p>

<pre><code>protected void SaveDataGridInTemp()
    {
        DataTable dtTemp = new DataTable();

        if (dtTemp == null || dtTemp.Columns.Count &lt;= 0)
        {
            dtTemp.Columns.Add("compname", typeof(String));
            dtTemp.Columns.Add("anam", typeof(String));
            dtTemp.Columns.Add("atype", typeof(String));
            dtTemp.Columns.Add("pri", typeof(int));
            dtTemp.Columns.Add("offrnam",typeof(String));
            dtTemp.Columns.Add("offrpri", typeof(int));
            dtTemp.Columns.Add("sd", typeof(String));
            dtTemp.Columns.Add("ed", typeof(String));
            dtTemp.PrimaryKey = new DataColumn[] { dtTemp.Columns["atype"] };

        }

        foreach (GridViewRow row in GridView1.Rows)
        {

            CheckBox cb = (CheckBox)row.FindControl("stockcb");
            if (cb != null &amp;&amp; cb.Checked)
            {
                DataRow drTemp = dtTemp.NewRow();
                drTemp["compname"] = GridView1.Rows[row.RowIndex].Cells[2].Text;
                drTemp["anam"] = GridView1.Rows[row.RowIndex].Cells[3].Text;
                drTemp["atype"] = GridView1.Rows[row.RowIndex].Cells[4].Text;
                drTemp["pri"] = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[5].Text);


                //Second Iteration

                foreach (GridViewRow row1 in GridView2.Rows)
                {
                    CheckBox cb1 = (CheckBox)row1.FindControl("offercb");

                    if (cb.Checked &amp;&amp; cb1.Checked)
                    {

                        //save data to temp table
                        drTemp["offrnam"] = GridView2.Rows[row1.RowIndex].Cells[1].Text;
                        drTemp["offrpri"] = Convert.ToInt32(GridView2.Rows[row1.RowIndex].Cells[2].Text);
                        drTemp["sd"] = GridView2.Rows[row1.RowIndex].Cells[3].Text;
                        drTemp["ed"] = GridView2.Rows[row1.RowIndex].Cells[4].Text;
                        dtTemp.Rows.Add(drTemp);

                        SqlConnection con = new SqlConnection(connect);
                        con.Open();
                        SqlCommand cmd;
                        if (dtTemp.Rows.Count &gt; 0)
                        {
                            for (int i = 0; i &lt; dtTemp.Rows.Count; i++)
                            {
                                cmd = new SqlCommand("insert into Rent_Set1  (company_name,ac_name,ac_type,ac_price,offername,offerprice,offerstartdate,offerenddate)"+ "values('" + dtTemp.Rows[1].ToString() + "','" + dtTemp.Rows[2].ToString() + "','" + dtTemp.Rows[4].ToString() + "','" + dtTemp.Rows[5].ToString() + "','" + dtTemp.Rows[6].ToString() + "','" + dtTemp.Rows[7].ToString() + "','" + dtTemp.Rows[8].ToString() + "','" + dtTemp.Rows[9].ToString() + "')");

                                cmd.ExecuteNonQuery();
                            }    
                        }
                    }
                }
            }
        }
    }
</code></pre>

<p>Upon executing this code i get an error saying <strong>"No row at position[1] at the cmd insert query line...,"</strong></p>

<p>Could somebody tell me what I'm doing wrong here...</p>
