In the GridView when in insert two lines(two records)then i click insert button it is inserting 4 records rather then two (its each row two time) and i have used 2 loops :one loops to check checkboxes are checked or not and another is for inserting the record in the database table.I want to enter the record which are avalable in the gridview and it should not be duplication.
here is my code:
int rowIndex = 0;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (CheckBox)row.FindControl("orditems");
if (chkRow.Checked)
{
if (ViewState["Sva_Medicines"] != null)
{
DataTable dt = (DataTable)ViewState["Sva_Medicines"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList Presc_id = GridView1.FooterRow.FindControl("txtprescnum") as DropDownList;
TextBox tmedname = GridView1.FooterRow.FindControl("txtmedname") as TextBox;
TextBox tmedbrand = GridView1.FooterRow.FindControl("txtmedbrand") as TextBox;
TextBox tmeduom = GridView1.FooterRow.FindControl("txtmeduom") as TextBox;
TextBox tmedprice = GridView1.FooterRow.FindControl("txtmedprice") as TextBox;
TextBox tmedcom = GridView1.FooterRow.FindControl("composition_txt") as TextBox;
TextBox medicine_name = GridView1.FooterRow.FindControl("txtmedname") as TextBox;
DropDownList Repeat_medicine = GridView1.FooterRow.FindControl("txtrepmed") as DropDownList;
DropDownList Morningflag = GridView1.FooterRow.FindControl("morningflag_ddl") as DropDownList;
DropDownList Afternoonflag = GridView1.FooterRow.FindControl("afternoonflag_ddl") as DropDownList;
DropDownList Eveningflag = GridView1.FooterRow.FindControl("eveningflag_ddl") as DropDownList;
DropDownList Dosage = GridView1.FooterRow.FindControl("Dosage_ddl") as DropDownList;
DropDownList Smsreminder = GridView1.FooterRow.FindControl("smsremainder_ddl") as DropDownList;
DropDownList Monthlyrefill = GridView1.FooterRow.FindControl("Monthlyrefill_ddl") as DropDownList;
TextBox Purpose = GridView1.FooterRow.FindControl("Purpose_txt") as TextBox;
TextBox tmedqty = GridView1.FooterRow.FindControl("txtmedqty") as TextBox;
Presc_id.SelectedItem.Text = dt.Rows[i]["Presc_num"].ToString();
tmedbrand.Text = dt.Rows[i]["Brand"].ToString();
tmeduom.Text = dt.Rows[i]["UOM"].ToString();
tmedqty.Text = dt.Rows[i]["Quantity"].ToString();
tmedprice.Text = dt.Rows[i]["Price"].ToString();
tmedcom.Text = dt.Rows[i]["Composition"].ToString();
medicine_name.Text = dt.Rows[i]["Medicine_name"].ToString();
Morningflag.Text = dt.Rows[i]["Morning_Flag"].ToString();
Afternoonflag.Text = dt.Rows[i]["Afternoon_Flag"].ToString();
Eveningflag.Text = dt.Rows[i]["Evening_Flag"].ToString();
Dosage.Text = dt.Rows[i]["Dosage"].ToString();
Smsreminder.Text = dt.Rows[i]["SMS_Remainder"].ToString();
Purpose.Text = dt.Rows[i]["Purpose"].ToString();
Repeat_medicine.Text = dt.Rows[i]["Repeat_medicine"].ToString();
SqlCommand cmd1 = new SqlCommand("insert into Sva_Medicines (Presc_id,Medicine_name,Brand,UOM,Quantity,Price,Repeat_medicine,Composition,Purpose,Dosage,SMS_Remainder,Morning_Flag,Afternoon_Flag,Evening_Flag,Monthly_Refill) Values(@Presc_id,@Medicine_name,@Brand,@UOM,@Quantity,@Price,@Repeat_medicine,@Composition,@Purpose,@Dosage,@SMS_Remainder,@Morning_Flag,@Afternoon_Flag,@Evening_Flag,@Monthly_Refill)", conne, trans);
cmd1.Parameters.AddWithValue("@Presc_id", Session["id"]);
cmd1.Parameters.AddWithValue("@Medicine_name", tmedname.Text);
cmd1.Parameters.AddWithValue("@Brand", tmedbrand.Text);
cmd1.Parameters.AddWithValue("@UOM", tmeduom.Text);
cmd1.Parameters.AddWithValue("@Quantity", tmedqty.Text);
cmd1.Parameters.AddWithValue("@Price", tmedprice.Text);
cmd1.Parameters.AddWithValue("@Composition", tmedcom.Text);
cmd1.Parameters.AddWithValue("@Purpose", Purpose.Text);
cmd1.Parameters.AddWithValue("@Dosage", Dosage.Text);
cmd1.Parameters.AddWithValue("@SMS_Remainder", Smsreminder.Text);
cmd1.Parameters.AddWithValue("@Morning_Flag", Morningflag.Text);
cmd1.Parameters.AddWithValue("@Afternoon_Flag", Afternoonflag.Text);
cmd1.Parameters.AddWithValue("@Evening_Flag", Eveningflag.Text);
cmd1.Parameters.AddWithValue("@Monthly_Refill", Monthlyrefill.Text);
cmd1.Parameters.AddWithValue("@Created_by", txt_mcreateby.ToString());
cmd1.Parameters.AddWithValue("@Created_date", txt_mcreatedate);
cmd1.Parameters.AddWithValue("@Updated_by", txt_mupdatedby.ToString());
cmd1.Parameters.AddWithValue("@Repeat_medicine", Repeat_medicine.Text);
cmd1.Parameters.AddWithValue("@Updated_date", txt_mupdateddate);
cmd1.ExecuteNonQuery();
Page.Controls.Add(new LiteralControl("<script>alert('Record inserted successfully');</script>"));
rowIndex++;
chkRow.Checked = false;
}
}
}
}
}
}