Dear All,
I have a Dynamic GridView on a page as below. The user can add or delete rows. When inserting record, the code is executed one row at a time. I have 3 rows to be inserted to the databse. Here is the issue:
if the first row is not completed, then no record is added to the database
if the first row is completed and the second row is not, then only the first row is inserted in the database
if the first row is completed, the second row is not complete, and the third row is completed, then only the first row is inserted in the database.
![Looping](http://www.datahus.net/Loop.jpg)
below is the code:
protected void GridView1_AddNewRecord(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["DatabaseVestConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd;
string query1 = "INSERT INTO kart_Bestilling(SakBehandlingCode, KystverketRegionID, KystverketAvdelingID, BestillerReferanse, BestillerLeveringAdresse, BestillerPostKodeBy, BestillerNavn, BestillerStilling, BestillerEpost, BestillerTelefon, BestillingBeskrivelse, BestillingDato, BehandlingSakNummer, BehandlingBeskrivelse, BehandlingStatus)" +
" VALUES (@SakBehandlingCode, @KystverketRegionID, @KystverketAvdelingID, @BestillerReferanse, @BestillerLeveringAdresse, @BestillerPostKodeBy, @BestillerNavn, @BestillerStilling, @BestillerEpost, @BestillerTelefon, @BestillingBeskrivelse, @BestillingDato, @BehandlingSakNummer, @BehandlingBeskrivelse, @BehandlingStatus)";
string query2 = "INSERT INTO kart_Order(kart_Bestilling.BestillingID, kart_Order.KartTypeName, kart_Order.KartNummerName, kart_Order.BestillingAntallKart1, kart_Order.KartPrisTall) VALUES(@BestillingID, @KartTypeName, @KartNummerName, @BestillingAntallKart1, @KartPrisTall)";
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
// 111111111111111111111111111111111111111111111111111111111111111111111111111111
DropDownList ddKartTypeName = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("dropDownKartTypeName");
DropDownList ddKartNummerName = (DropDownList)Gridview2.Rows[rowIndex].Cells[2].FindControl("dropDownKartNummerName");
TextBox tBestillingAntallKart1 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("txtBestillingAntallKart1");
// 111111111111111111111111111111111111111111111111111111111111111111111111111111
if (txtBestillerReferanse.Text != "" && txtBestillerEpost.Text != "" && txtBestillerNavn.Text != "" && txtBestillerPostKodeBy.Text != "" && ddKartTypeName.SelectedValue != "Velg KartType" && ddKartNummerName.SelectedValue != "Velg KartNummer" && tBestillingAntallKart1.Text != "")
{
if (GridView1.Rows.Count >= 0)
{
GridView1.AllowPaging = false; //Remove the paging
try
{
con.Open();
cmd = new SqlCommand(query1);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@SakBehandlingCode", txtSakBehandlingCode.Text.Trim());
cmd.Parameters.AddWithValue("@KystverketRegionID", dropDownKystverketRegionNavn.SelectedValue);
cmd.Parameters.AddWithValue("@KystverketAvdelingID", dropDownKystverketAvdelingNavn.SelectedValue);
cmd.Parameters.AddWithValue("@BestillerReferanse", txtBestillerReferanse.Text.Trim());
cmd.Parameters.AddWithValue("@BestillerLeveringAdresse", txtBestillerLeveringAdresse.Text.Trim());
cmd.Parameters.AddWithValue("@BestillerPostKodeBy", txtBestillerPostKodeBy.Text.Trim());
cmd.Parameters.AddWithValue("@BestillerNavn", txtBestillerNavn.Text.Trim());
cmd.Parameters.AddWithValue("@BestillerStilling", txtBestillerStilling.Text.Trim());
cmd.Parameters.AddWithValue("@BestillerEpost", txtBestillerEpost.Text.Trim());
cmd.Parameters.AddWithValue("@BestillerTelefon", txtBestillerTelefon.Text.Trim());
cmd.Parameters.AddWithValue("@BestillingBeskrivelse", txtBestillingBeskrivelse.Text.Trim());
cmd.Parameters.AddWithValue("@BestillingDato", txtBestillingDato.Text.Trim());
cmd.Parameters.AddWithValue("@BehandlingSakNummer", txtBehandlingSakNummer.Text.Trim());
cmd.Parameters.AddWithValue("@BehandlingBeskrivelse", txtBehandlingBeskrivelse.Text.Trim());
cmd.Parameters.AddWithValue("@BehandlingStatus", dropDownBehandlingStatus.SelectedValue);
cmd.ExecuteNonQuery();
GridView1_BindData();
GridView1_BindData2();
}
catch (Exception)
{
lblMessage.Text = "--- Opps! Bestilling Annullert: Glemt Region/Avdeling?";
}
finally
{
con.Close();
}
if (lblMessage.Text != "--- Opps! Bestilling Annullert: Glemt Region/Avdeling?")
{
DropDownList ddKartPrisTall = (DropDownList)Gridview2.Rows[rowIndex].Cells[3].FindControl("dropDownKartPrisTall");
if (ddKartTypeName.SelectedValue.Equals("PapirKart"))
{ ddKartPrisTall.SelectedValue = "128.25"; }
else if (ddKartTypeName.SelectedValue.Equals("NorskeLos"))
{ ddKartPrisTall.SelectedValue = "178"; }
else if (ddKartTypeName.SelectedValue.Equals("TideVann"))
{ ddKartPrisTall.SelectedValue = "82"; }
else if (ddKartTypeName.SelectedValue.Equals("EFS Abonnement"))
{ ddKartPrisTall.SelectedValue = "608"; }
else if (ddKartTypeName.SelectedValue.Equals("Kartkatalog"))
{ ddKartPrisTall.SelectedValue = "0"; }
else if (ddKartTypeName.SelectedValue.Equals("Symboler og Forkortelser"))
{ ddKartPrisTall.SelectedValue = "136"; }
else if (ddKartTypeName.SelectedValue.Equals("Antikvariske Sjokart"))
{ ddKartPrisTall.SelectedValue = "80"; }
else if (ddKartTypeName.SelectedValue.Equals("Kart til Skolebruk"))
{ ddKartPrisTall.SelectedValue = "50"; }
try
{
con.Open();
cmd = new SqlCommand(query2);
cmd.Connection = con;
// Last Row ID in the table
Int32 index = GridView1.Rows.Count - 1;
string bestillingId = GridView1.DataKeys[index].Values["BestillingID"].ToString();
cmd.Parameters.AddWithValue("@BestillingID", bestillingId);
cmd.Parameters.AddWithValue("@KartTypeName", ddKartTypeName.SelectedValue);
cmd.Parameters.AddWithValue("@KartNummerName", ddKartNummerName.SelectedValue);
cmd.Parameters.AddWithValue("@BestillingAntallKart1", tBestillingAntallKart1.Text.Trim());
cmd.Parameters.AddWithValue("@KartPrisTall", ddKartPrisTall.SelectedValue);
cmd.ExecuteNonQuery();
}
catch (Exception)
{
lblMessage.Text = "Vennligst angi kartType / KartNummer / Antall";
}
finally
{
con.Close();
}
//GridView1_BindData();
// GridView1_BindData2();
rowIndex++;
}
}
GridView1_BindData();
GridView1_BindData2();
QueryResult.Text = "Record Added successfully!";
lblMessage.Text = "";
// Disable Submit Button
btnAdd.Enabled = false;
GridView1.AllowPaging = true; // Add the paging
}
else
{
lblMessage.Text = "Bestilling Mislykket. Husk Obligatorisk Felt! Sjekk kartType / KartNummer / Antall";
}
}
}
}
}
How to arrange this piece of code to validate all rows before any insert? or avoid inserting a row if there is one row not complete?
I am struggling with this. the conditions starts with int rowIndex = 0;
if (txtBestillerReferanse.Text != "" && txtBestillerEpost.Text != "" && txtBestillerNavn.Text != "" && txtBestillerPostKodeBy.Text != "" && ddKartTypeName.SelectedValue != "Velg KartType" && ddKartNummerName.SelectedValue != "Velg KartNummer" && tBestillingAntallKart1.Text != "")
Many thanks