代码执行不下降

时间:2018-07-02 09:25:35

标签: c# .net

我下面有一个代码,它的行为很奇怪,关闭While循环后的代码行未执行。如果我在While循环之前写这些行,它们就会执行。我在这个问题上进行了很多搜索,但都失败了。

if ((Convert.ToInt32(newDt.Rows[i]["qty"])) > (Convert.ToInt32(newDt1.Rows[i]["qty"])))
            {
                k_batch = Convert.ToInt32(newDt.Rows[i]["batch_num"]);
                label20.Content = k_batch.ToString();
                var dqty = (from row in Addition_result.AsEnumerable()      //retriving value from DataTable
                            where row.Field<int>("batch_num") == k_batch
                            select row.Field<int>("qty")).FirstOrDefault();

                dqty_y = Convert.ToInt32(dqty);
                label16.Content = dqty_y.ToString();


                    con.Open();

                    SqlCommand cmd1 = new SqlCommand("SELECT quantity,sold_qty,left_qty FROM batch WHERE id='" + k_batch + "'", con);
                    SqlDataReader batch_qty_details = null;
                    batch_qty_details = cmd1.ExecuteReader();

                    while (batch_qty_details.Read())
                    {
                        label16.Content = dqty_y.ToString();
                        batch_qty = Convert.ToInt32(batch_qty_details["quantity"]);
                        batch_left = Convert.ToInt32(batch_qty_details["left_qty"]);
                        batch_sold = Convert.ToInt32(batch_qty_details["sold_qty"]);
                    }  //code after this is not executing

                label18.Content = batch_left.ToString();
                label19.Content = batch_sold.ToString();
                label21.Content = dqty_y.ToString();
                label22.Content = batch_qty.ToString();
                label16.Content = batch_sold + dqty_y;
                label17.Content = batch_left - dqty_y;

                if (((batch_sold + dqty_y) <= batch_qty) && ((batch_left - dqty_y) >= 0))
                {
                    SqlCommand command = new SqlCommand("update batch set sold_qty=sold_qty+@soldqty2, left_qty=left_qty-@soldqty2 where id=@id2", con);
                    command.Parameters.AddWithValue("@soldqty2", Convert.ToInt32(dqty_y));
                    command.Parameters.AddWithValue("@id2", Convert.ToInt32(k_batch));
                    rexe = command.ExecuteNonQuery();
                }
                else
                {
                    MessageBox.Show("Please Check you Ordered Quantity !");
                }



                con.Close();

            }
        }

1 个答案:

答案 0 :(得分:1)

实际上存在一个问题,即连接状态已经打开,而我试图再次打开它。因此,然后我使用了try-catch块,它告诉我连接已经打开。现在,我更改了代码,就在这里。

if ((Convert.ToInt32(newDt.Rows[i]["qty"])) > (Convert.ToInt32(newDt1.Rows[i]["qty"])))
            {
                k_batch = Convert.ToInt32(newDt.Rows[i]["batch_num"]);
                label20.Content = k_batch.ToString();
                var dqty = (from row in Addition_result.AsEnumerable()      //retriving value from DataTable
                            where row.Field<int>("batch_num") == k_batch
                            select row.Field<int>("qty")).FirstOrDefault();

                dqty_y = Convert.ToInt32(dqty);
                label16.Content = dqty_y.ToString();

                try
                {
                    //con.Open();

                    SqlCommand cmd1 = new SqlCommand("SELECT quantity,sold_qty,left_qty FROM batch WHERE id='" + k_batch + "'", con);
                    SqlDataReader batch_qty_details = null;
                    batch_qty_details = cmd1.ExecuteReader();

                    while (batch_qty_details.Read())
                    {
                        label16.Content = dqty_y.ToString();
                        batch_qty = Convert.ToInt32(batch_qty_details["quantity"]);
                        batch_left = Convert.ToInt32(batch_qty_details["left_qty"]);
                        batch_sold = Convert.ToInt32(batch_qty_details["sold_qty"]);
                    }
                    con.Close();
                }
                catch(Exception EX)
                {
                    MessageBox.Show(EX.ToString());

                }
                label18.Content = batch_left.ToString();
                label19.Content = batch_sold.ToString();
                label21.Content = dqty_y.ToString();
                label22.Content = batch_qty.ToString();
                label16.Content = batch_sold + dqty_y;
                label17.Content = batch_left - dqty_y;


                if (((batch_sold + dqty_y) <= batch_qty) && ((batch_left - dqty_y) >= 0))
                {
                    con.Open();
                    SqlCommand command = new SqlCommand("update batch set sold_qty=sold_qty+@soldqty2, left_qty=left_qty-@soldqty2 where id=@id2", con);
                    command.Parameters.AddWithValue("@soldqty2", Convert.ToInt32(dqty_y));
                    command.Parameters.AddWithValue("@id2", Convert.ToInt32(k_batch));
                    rexe = command.ExecuteNonQuery();
                }
                else
                {
                    check_qty = -1;


                }






            }