addnewrow問題

小裕
如何讓找不到品號的時候不執行addnewrow?
現在是寫在迴圈外面




      string ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["readerConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();

            foreach (GridViewRow rw in this.gv.Rows)
            {

                TextBox tTextBox1 = (TextBox)rw.FindControl("item_no");
                TextBox tTextBox2 = (TextBox)rw.FindControl("item_name");
                TextBox tTextBox3 = (TextBox)rw.FindControl("brand");
                TextBox tTextBox4 = (TextBox)rw.FindControl("MB064");
                TextBox tTextBox5 = (TextBox)rw.FindControl("MC007");
                TextBox tTextBox6 = (TextBox)rw.FindControl("qty");

                SqlCommand command = new SqlCommand("SELECT MB001,MB002,MB003,CAST(MB064 as decimal(16,0)) as MB064,ISNULL(CAST(MC007 as decimal(16,0)),0) as MC007 from INVMB a LEFT JOIN (SELECT MC001,MC002, MC007 FROM INVMC WHERE MC002='1002')b ON a.MB001=b.MC001  where a.MB001='" + tTextBox1.Text + "'", conn);
                //conn.Open(); 
                SqlDataReader reader1;
                reader1 = command.ExecuteReader();

                if (reader1.Read())
                {
                    tTextBox2.Text = reader1["MB002"].ToString();
                    tTextBox3.Text = reader1["MB003"].ToString();
                    tTextBox4.Text = reader1["MB064"].ToString();
                    tTextBox5.Text = reader1["MC007"].ToString();
                    string r_qty = reader1["MB064"].ToString();
                    string t_qty = reader1["MC007"].ToString();

                    double rr_qty;
                    if (double.TryParse(tTextBox4.Text, out rr_qty))
                    {
                        rr_qty = Convert.ToDouble(((TextBox)rw.FindControl("MB064") as TextBox).Text);
                        //Convert.ToDouble(((Label)rw.FindControl("MC007") as Label).Text);
                    }
                    else
                    {
                        rr_qty = 0;
                    }

                    double tt_qty;
                    if (double.TryParse(tTextBox5.Text, out tt_qty))
                    {
                        tt_qty = Convert.ToDouble(((TextBox)rw.FindControl("MC007") as TextBox).Text);
                    }
                    else
                    {
                        tt_qty = 0;
                    }

                    //double a = Convert.ToDouble(rr_qty.ToString());
                    //double b = Convert.ToDouble(tt_qty.ToString());
                    double c = rr_qty + tt_qty;
                    //庫存總量
                    tTextBox6.Text = c.ToString();
                    //tTextBox6.Text = c.ToString("#,0");

                }
                else
                {
                    String TransferPage;
                    TransferPage = "<script>alert('找不到該品號!')</script>";
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "temp", TransferPage, false);
                    tTextBox1.Text = "";

                }
                reader1.Close();

            }
            AddNewRow();
          
            LastIndex = gv.Rows.Count + 1;
            //hfLastIndex.Value = LastIndex.ToString();

            UpdatePanel1.Update();
            ScriptManager1.SetFocus("gv$ctl" + string.Format("{0:00}", LastIndex) + "$item_no");


        }

小裕
我嘗試寫成這樣,可以成功顯示


   string ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["TAROKOConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();
            int FinalIndex = 0;

            foreach (GridViewRow rw in this.gv.Rows)
            {

                TextBox tTextBox1 = (TextBox)rw.FindControl("item_no");
                TextBox tTextBox2 = (TextBox)rw.FindControl("item_name");
                TextBox tTextBox3 = (TextBox)rw.FindControl("brand");
                TextBox tTextBox4 = (TextBox)rw.FindControl("MB064");
                TextBox tTextBox5 = (TextBox)rw.FindControl("MC007");
                TextBox tTextBox6 = (TextBox)rw.FindControl("qty");

                SqlCommand command = new SqlCommand("SELECT MB001,MB002,MB003,CAST(MB064 as decimal(16,0)) as MB064,ISNULL(CAST(MC007 as decimal(16,0)),0) as MC007 from INVMB a LEFT JOIN (SELECT MC001,MC002, MC007 FROM INVMC WHERE MC002='1002')b ON a.MB001=b.MC001  where a.MB001='" + tTextBox1.Text + "'", conn);
                //conn.Open(); 
                SqlDataReader reader1;
                reader1 = command.ExecuteReader();

                if (reader1.Read())
                {
                 
                    tTextBox2.Text = reader1["MB002"].ToString();

                    tTextBox3.Text = reader1["MB003"].ToString();
                    tTextBox4.Text = reader1["MB064"].ToString();
                    tTextBox5.Text = reader1["MC007"].ToString();
                    string r_qty = reader1["MB064"].ToString();
                    string t_qty = reader1["MC007"].ToString();

                   // ViewState["aa"] = reader1["MB002"].ToString();
                    //Response.Write(tTextBox2.Text);

                    double rr_qty;
                    if (double.TryParse(tTextBox4.Text, out rr_qty))
                    {
                        rr_qty = Convert.ToDouble(((TextBox)rw.FindControl("MB064") as TextBox).Text);
                        //Convert.ToDouble(((Label)rw.FindControl("MC007") as Label).Text);
                    }
                    else
                    {
                        rr_qty = 0;
                    }

                    double tt_qty;
                    if (double.TryParse(tTextBox5.Text, out tt_qty))
                    {
                        tt_qty = Convert.ToDouble(((TextBox)rw.FindControl("MC007") as TextBox).Text);
                    }
                    else
                    {
                        tt_qty = 0;
                    }

                    //double a = Convert.ToDouble(rr_qty.ToString());
                    //double b = Convert.ToDouble(tt_qty.ToString());
                    double c = rr_qty + tt_qty;
                    //庫存總量
                    tTextBox6.Text = c.ToString();

                     hfLastIndex0.Value = "Y";
   

                }
                else
                {
                    String TransferPage;
                    TransferPage = "<script>alert('找不到該品號!')</script>";
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "temp", TransferPage, false);
                    tTextBox1.Text = "";

                     
                    hfLastIndex0.Value = "N";
    


                }
                reader1.Close();

            }

            if (hfLastIndex0.Value == "Y")
            {

                AddNewRow();
                FinalIndex = gv.Rows.Count + 1;

                UpdatePanel1.Update();
                ScriptManager1.SetFocus("gv$ctl" + string.Format("{0:00}", FinalIndex) + "$item_no");
            }
            else
            {

            }

回到頂部