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:
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("@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);
}
foreach (GridViewRow row in GridViewNewHost.Rows)
{
GridView GridChild1 = row.FindControl("gvPatchPanel1") as GridView;
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);
GridView GridChild2 = row.FindControl("gvPatchPanel2") as GridView;
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;
}
}