两个循环输入4个记录,但GridView中只有两个记录。 [英] Two loops entering 4 record but there are only two records in the GridView.

查看:88
本文介绍了两个循环输入4个记录,但GridView中只有两个记录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GridView中插入两行(两个记录)然后我点击插入按钮它插入4个记录而不是两个(每行两次)我使用了2个循环:一个循环检查复选框被选中或不是和另一个是在数据库表中插入记录。我想输入在gridview中可用的记录,它不应该重复。

这里是我的代码:

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 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;

                   }

                }


            }

        }
    }
}

推荐答案

你得到四个插入而不是两个,因为这正是你要求的。

迭代每个网格视图行( foreach(GridView1.Rows中的GridViewRow行)),并且对于每个迭代,您再次遍历每个数据表行( for(int i = 0; i< dt.Rows.Count; i ++))。



最好我可以给你的建议是学习如何调试你的代码。

你应该看看打入代码,步进或运行代码,设置下一个要执行的语句 [ ^ ]。

了解有关调试的基本概念不是可选的。



你SH应该尝试:

- 从你的外循环中声明你的 SqlCommand 对象,最好是在中使用声明。

- 初始化命令的参数(例如 command.Parameters.Add(@ Presc_id))。 />
- 摆脱你的内循环。

- 在每次迭代时,限定每个参数( command.Parameters [@ Medicine_name]。Value = tmedname .Text; ,例如),并执行查询。



但是,再一次,对你来说最重要的是跟随你关于调试的基本方法。
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.


这篇关于两个循环输入4个记录,但GridView中只有两个记录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆