none
Why are the results changing every other call?? RRS feed

  • Question

  • here we using if statement and it's work fine for first time

    then when i enter barcode and pressed enter again it's going direct to else statement it's mean shown me error message under as we see on else statement and i think it's not repeat whole statement as new process because i'm sure from condition it's true and it shouldn't go to else statement but in third time when i try enter barcode and check from condition it's work fine like first time and then in forth time show me error again

    why error message shown on every second try

     block is this line:

    if (dt.Rows.Count > 0)

    here whole code

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (checkBox1.Checked)
            {
    
                if (qtystate == true)
                {
                    DataTable dt;
                    barcode = textBox1.Text;
                    dt = pro.GetProdcutBar(textBox1.Text);
    
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            barcode = row[0].ToString();
                            itemno = row[1].ToString();
                            itemname = row[2].ToString();
                            salespric = row[3].ToString();
                            costprice = row[4].ToString();
                        }
                        cont.addprodcuttocount(Convert.ToString(barcode), Convert.ToString(itemno), Convert.ToString(itemname),
                            Convert.ToString(salespric), Convert.ToString(costprice), "1", DateTime.Now, SystemInformation.ComputerName);
                        refershgridview();
    
                        //sum qty
                        try
                        {
                            int sum = 0;
                            for (int i = 0; i < dataGridView1.Rows.Count; ++i)
                            {
                                if (Convert.ToString(dataGridView1.Rows[i].Cells[1].Value) == textBox1.Text)
                                {
                                    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[6].Value);
                                }
    
                            }
                            label3.Text = "Barcode " + barcode + " - " + "Item NO" + itemno + " - " + "item name " + itemname + " - " + "qty  " + sum.ToString();
                        }
    
                        catch
                        {
    
                        }
    
                        barcode = "";
                        itemno = "";
                        itemname = "";
                        salespric = "";
                        costprice = "";
                        qtyitem = "";
                        textBox1.Clear();
                        textBox1.Focus();
                    }
    
                    else
                    {
                        MessageBox.Show("00barcode not found", "barcode", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        barcode = "";
                        itemno = "";
                        itemname = "";
                        salespric = "";
                        costprice = "";
                        qtyitem = "";
                        textBox1.Clear();
                        textBox1.Focus();
                    }
    
                }
    
            }
    
        }
    }



    • Edited by Mohammed mqi Wednesday, November 7, 2018 10:59 AM add more info
    Wednesday, November 7, 2018 10:58 AM

All replies

  • Hi Mohammed mqi,

    According to your description and related code, I would suggest that you check the following method on your application, which return datatable that does not have any records at second try.

    In addition, you could check if the variable named textBox1.Text has value at second try.


     dt = pro.GetProdcutBar(textBox1.Text);

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 8, 2018 1:18 AM
    Moderator