Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a nested loop to iterate through a gridview and nested gridviews and insert into a database. The insert works properly as long as there is only one row in the main grid view, when I add a second row to the main view, I get errors saying that I already have the parameter defined. this same code works well when I am not using nested grid views elsewhere. I have been trying to fix this piece of code now for weeks and getting no where. See below code I'm using to insert and see some of my comments of stuff I have tried.

What I have tried:

C#
protected void ButtonHostSubmit_Click(object sender, EventArgs e)
        {

            SqlDataSourceNewRequest.Insert();

            try
            {

                String ConStr = ConfigurationManager.ConnectionStrings["StorageRequestConnectionString"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(ConStr))
                {
                    using (SqlCommand cmd = new SqlCommand("insert into NewHost_Details ([RequestID],[Hostname],[Clustername],[IPAddress],[HBA1PortName],[HBA1Vendor],[HBA1Model],[HBA1Speed],[HBA1Rack],[HBA1Tray],[HBA1Row],[HBA1Section]," +
                        "[HBA2PortName],[HBA2Vendor],[HBA2Model],[HBA2Speed],[HBA2Rack],[HBA2Tray],[HBA2Row],[HBA2Section]) " +
                        "values (@RequestID, @Hostname,@Clustername, @IPAddress, @HBA1PortName, @HBA1Vendor, @HBA1Model ,@HBA1Speed, @HBA1Rack, @HBA1Tray, @HBA1Row, @HBA1Section," +
                        "@HBA2PortName,@HBA2Vendor,@HBA2Model,@HBA2Speed,@HBA2Rack,@HBA2Tray,@HBA2Row,@HBA2Section)", conn))
                    {
                        int amt = Convert.ToInt32(DropDownListHostCount.SelectedValue);
                        conn.Open();
                        for (int i = 0; i < amt; i++)
                        {
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@RequestID", LabelSessionID.Text);
                            cmd.Parameters.AddWithValue("@Hostname", ((DropDownList)GridViewNewHost.Rows[i].Cells[1].FindControl("dlHostname")).SelectedValue);
                            // cmd.Parameters.AddWithValue("@Clustername", ((TextBox)GridViewNewHost.Rows[i].Cells[2].FindControl("txtClusterName")).Text);
                            cmd.Parameters.AddWithValue("@IPAddress", ((TextBox)GridViewNewHost.Rows[i].Cells[2].FindControl("txtIP")).Text);
                            if (dlJoinCluster.SelectedItem.Value == "New")
                            {
                                cmd.Parameters.AddWithValue("@Clustername", ((TextBox)GridViewNewHost.Rows[i].Cells[3].FindControl("Clustername")).Text);
                            }
                            else
                            if (dlJoinCluster.SelectedItem.Value == "Existing")
                            {
                                cmd.Parameters.AddWithValue("@Clustername", ((DropDownList)GridViewNewHost.Rows[i].Cells[3].FindControl("Clustername")).SelectedValue);
                            }

                            //Need to assingn a variable to [0] i


                            foreach (GridViewRow row in GridViewNewHost.Rows)
                            {
                                GridView GridChild1 = row.FindControl("gvPatchPanel1") as GridView;
                                // foreach (GridViewRow row1 in GridChild1.Rows)
                                //  {
                              

                                //cmd.Parameters.Clear();
                                    cmd.Parameters.AddWithValue("@HBA1PortName", ((TextBox)GridChild1.Rows[i].Cells[0].FindControl("txtHBA1WWN")).Text);
                                    cmd.Parameters.AddWithValue("@HBA1Vendor", ((TextBox)GridChild1.Rows[i].Cells[1].FindControl("txtHBA1Vendor")).Text);
                                    cmd.Parameters.AddWithValue("@HBA1Model", ((TextBox)GridChild1.Rows[i].Cells[2].FindControl("txtHBA1Model")).Text);
                                    cmd.Parameters.AddWithValue("@HBA1Speed", ((DropDownList)GridChild1.Rows[i].Cells[3].FindControl("ddlHBA1Speed")).SelectedValue);
                                    cmd.Parameters.AddWithValue("@HBA1Rack", ((TextBox)GridChild1.Rows[i].Cells[4].FindControl("txtHBA1Rack")).Text);
                                    cmd.Parameters.AddWithValue("@HBA1Tray", ((TextBox)GridChild1.Rows[i].Cells[5].FindControl("txtHBA1Tray")).Text);
                                    cmd.Parameters.AddWithValue("@HBA1Row", ((TextBox)GridChild1.Rows[i].Cells[6].FindControl("txtHBA1Row")).Text);
                                    cmd.Parameters.AddWithValue("@HBA1Section", ((TextBox)GridChild1.Rows[i].Cells[7].FindControl("txtHBA1Section")).Text);

                               // }
                            //}

                           // foreach (GridViewRow row in GridViewNewHost.Rows)
                           // {
                                GridView GridChild2 = row.FindControl("gvPatchPanel2") as GridView;
                              //  foreach (GridViewRow row2 in GridChild2.Rows)
                              //  {
                                    
                                   // cmd.Parameters.Clear();
                                    cmd.Parameters.AddWithValue("@HBA2PortName", ((TextBox)GridChild2.Rows[i].Cells[0].FindControl("txtHBA2WWN")).Text);
                                    cmd.Parameters.AddWithValue("@HBA2Vendor", ((TextBox)GridChild2.Rows[i].Cells[1].FindControl("txtHBA2Vendor")).Text);
                                    cmd.Parameters.AddWithValue("@HBA2Model", ((TextBox)GridChild2.Rows[i].Cells[2].FindControl("txtHBA2Model")).Text);
                                    cmd.Parameters.AddWithValue("@HBA2Speed", ((DropDownList)GridChild2.Rows[i].Cells[3].FindControl("ddlHBA2Speed")).SelectedValue);
                                    cmd.Parameters.AddWithValue("@HBA2Rack", ((TextBox)GridChild2.Rows[i].Cells[4].FindControl("txtHBA2Rack")).Text);
                                    cmd.Parameters.AddWithValue("@HBA2Tray", ((TextBox)GridChild2.Rows[i].Cells[5].FindControl("txtHBA2Tray")).Text);
                                    cmd.Parameters.AddWithValue("@HBA2Row", ((TextBox)GridChild2.Rows[i].Cells[6].FindControl("txtHBA2Row")).Text);
                                    cmd.Parameters.AddWithValue("@HBA2Section", ((TextBox)GridChild2.Rows[i].Cells[7].FindControl("txtHBA2Section")).Text);


                               // }
                            }

                            cmd.ExecuteNonQuery();
                        }
                        conn.Close();
                        Response.Redirect("Pending1.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.ToString();
                throw ex;
            }

        }
Posted
Updated 9-Sep-22 10:36am
v2

1 solution

Why are you using loops at all?
Instead of issuing multiple INSERT operations (one per row) construct a DataTable instead, and use the SqlBulkCopy Class (System.Data.SqlClient) | Microsoft Docs[^] to bulk INSERT the whole lot as one operation.
I'd also suggest that you want to use an SqlTransaction around the whole operation, so that is one row fails they can all be rolled back instead of leaving the DB in an indeterminate state.
 
Share this answer
 
Comments
jovon44 12-Sep-22 10:53am    
Thanks for your suggestion.
Now when I add in the bulk copy and try to get the nested grid views it cant find them in the context. not sure what I am doing wrong.
Thanks again in advance

protected void Bulk_Insert(object sender, EventArgs e)
{
DataTable dt = new DataTable();

dt.Columns.AddRange(new DataColumn[20] {
new DataColumn("RequestID", typeof(string)),
new DataColumn("Host Name", typeof(string)),
new DataColumn("IP", typeof(string)),
new DataColumn("Cluster", typeof(string)),
new DataColumn("HBA1 WWN Port Name",typeof(string)),
new DataColumn("HBA1 Vendor",typeof(string)),
new DataColumn("HBA1 Model",typeof(string)),
new DataColumn("HBA1 Speed",typeof(string)),
new DataColumn("hba1Rack", typeof(string)),
new DataColumn("hba1Tray", typeof(string)),
new DataColumn("hba1Row", typeof(string)),
new DataColumn("hba1Section",typeof(string)),
new DataColumn("HBA2 WWN Port Name",typeof(string)),
new DataColumn("HBA2 Vendor",typeof(string)),
new DataColumn("HBA2 Model",typeof(string)),
new DataColumn("HBA2 Speed",typeof(string)),
new DataColumn("hba2Rack", typeof(string)),
new DataColumn("hba2Tray", typeof(string)),
new DataColumn("hba2Row", typeof(string)),
new DataColumn("hba2Section",typeof(string)),

});

foreach (GridViewRow row in GridView1.Rows) // Master Gris
{

string RequestID = LabelSessionID.Text;
string host = row.Cells[1].Text;
string ip = row.Cells[2].Text;
string Cluster = row.Cells[3].Text;

GridView GridChild1 = row.FindControl("gvPatchPanel1") as GridView;
// foreach (GridViewRow row1 in GridChild1.Rows) // nested grid 1
{

string HBA1PortName = row1.Cells[0].Text;
string HBA1Vendor = row1.Cells[1].Text;
string HBA1Model = row1.Cells[2].Text;
string HBA1Speed = row1.Cells[3].Text;
string HBA1Rack = row1.Cells[4].Text;
string HBA1Tray = row1.Cells[5].Text;
string HBA1Row = row1.Cells[6].Text;
string HBA1Section = row1.Cells[7].Text;

}

GridView GridChild2 = row.FindControl("gvPatchPanel2") as GridView;
foreach (GridViewRow row2 in GridChild2.Rows) //Nested Grid2
{

string HBA2PortName = row2.Cells[0].Text;
string HBA2Vendor = row2.Cells[1].Text;
string HBA2Model = row2.Cells[2].Text;
string HBA2Speed = row2.Cells[3].Text;
string HBA2Rack = row2.Cells[4].Text;
string HBA2Tray = row2.Cells[5].Text;
string HBA2Row = row2.Cells[6].Text;
string HBA2Section = row2.Cells[7].Text;

}


dt.Rows.Add(RequestID, host, ip, Cluster, HBA1PortName, HBA1Vendor, HBA1Model, HBA1Speed, HBA1Rack, HBA1Tray, HBA1Row, HBA1Section, HBA2PortName, HBA2Vendor, HBA2Model, HBA2Speed, HBA2Rack, HBA2Tray, HBA2Row, HBA2Section);

}

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