Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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 loop for extract values
                    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;

                   }

                }


            }

        }
    }
}
Posted
Updated 3-Jul-15 21:19pm
v2

1 solution

You get four inserts instead of two because this is exactly what you are asking to.
You iterate over each grid view row (foreach (GridViewRow row in GridView1.Rows)), and for each of these iterations you iterate again over each datatable row (for (int i = 0; i < dt.Rows.Count; i++)).

The best piece of advice that I could give you is to learn how to debug your code.
You should have a look at Break into code, step or run through code, set the next statement to execute[^].
Understanding basic concepts about debugging is not optional.

You should try to:
- declare your SqlCommand object out of your outer loop, preferably in a using statement.
- initialize the command's parameters (command.Parameters.Add("@Presc_id"), for example).
- get rid of your inner loop.
- at each iteration, qualify each parameter (command.Parameters["@Medicine_name"].Value = tmedname.Text;, for example), and execute the query.

But, once again, the most important for you now is to follow a basic how-to about debugging.
 
Share this answer
 

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