Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:


C#
<pre>if (dt.Rows.Count > 0)


here whole code


C#
<pre>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();
                }

            }

        }

    }
}


What I have tried:

i try to remove try blocks and nothing happens
Posted
Updated 7-Nov-18 0:17am
Comments
F-ES Sitecore 7-Nov-18 6:10am    
Use the debugger to step through your code to see what is happening.
mohammed mqi 7-Nov-18 7:04am    
when i debug my code it's give me one result
Richard MacCutchan 7-Nov-18 8:27am    
Why are you using Convert.ToString on all the items that have previously been returned in string form? Also you set barcode equal to textBox1.Text, but then use that same text to create your datatable.
mohammed mqi 7-Nov-18 9:05am    
thanks Richard for replay i'm using Convert.ToString to be more insure and becuase some time i revived float value
and about barcode equal to textBox1.Text becuase i'm using textBox1.Text for manual to put qty manul in other case after receiving barcode first then if qty empty it will be pass qty from textBox1.Text when switch case to inserting qty

1 solution

We can't tell you, because we don't know exactly what you are doing, where the barcode info is going, ... and we can't run your code to find out (we don't have your scanner, your pro class, or any idea what it's GetProdcutBar method is doing.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger - a quick Google for "Visual Studio debugger" should give you the info you need to get started with it.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But before you do that, stop swallowing exceptions!
When you write code like this:
try
{
    int sum = 0;
    for (int i = 0; i < dataGridView1.Rows.Count; ++i)
    ...
}

catch
{

}
The chances are that you are throwing away information you really do need in order to fix problems. Either don't use a try...catch block at all, or use the Exception object to help you fix problems: log it, show it to the user, output it to the debug console, whatever - but just catching everything and ignoring it means you don't even know that a problem has happened until it shows up as a problem elsewhere, or elsewhen...
 
Share this answer
 
Comments
mohammed mqi 7-Nov-18 6:58am    
thanks for helping
by the way i'm insert barcode manually
i remove all Exception but stil results changing every call
but when i debug my code it's give me one result
OriginalGriff 7-Nov-18 7:14am    
And what happens the second time? What does the debugger show you?
mohammed mqi 7-Nov-18 7:32am    
it's give all correct result not reach to else statement at all with debugger only
and that what i want
but without debugger it's show me other result that's mean it's go to else statement
OriginalGriff 7-Nov-18 7:41am    
If that's what you want, then what are you complaining about? :laugh:

Remember, I can't see your screen, I can't access your HDD, and I can't read your mind ... I only get exactly what you type to work with.

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