1

I am writing a scanning program using visual studio.

As shown as following code, I do some checking on the textbox (name as txt_item), one of the checking is to check the existence of the item scanned in the database. If the item exist in the database, it will do something. Else it will prompt out an error message to ask the packer to re-scan the barcode and clear the content in the "txt_item" textbox.

The messagebox should be prompt out at one time, but now it is appear twice. Does anyone know how do I fix this?

private void txt_item_TextChanged(object sender, EventArgs e)
{
    SqlConnection Conn = Global_Variable.GetConnectionString();
    SqlCommand cmd;

    cmd = new SqlCommand("Select  statement...", Conn);

    try
    {
        if (Conn.State == ConnectionState.Closed)
        {
            Conn.Open();
        }

        cmd.CommandType = CommandType.Text;

        SqlDataReader sdr = cmd.ExecuteReader();

        if (sdr.HasRows != null && sdr.HasRows)
        {
            cmd.Dispose();
            sdr.Dispose();

            SqlDataAdapter itemDataAdapter = new SqlDataAdapter(cmd);
            DataTable itemDataTable = new DataTable();

            itemDataTable.Rows.Clear();
            itemDataAdapter.Fill(itemDataTable);

            // blah blah blah...

            return;
        }
        else
        {
            if (MessageBox.Show("Invalid Item, Please re-enter again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
            {
                txt_item.Clear(); ;
            }
             return;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        cmd.Dispose();

        if (Conn.State == ConnectionState.Open)
        {
            Conn.Close();
        }
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Empty
  • 123
  • 3
  • 11
  • `TextChanged` event occures on every new symbol. So, if you press 2 keys, you'll get 2 messages – Backs Nov 11 '15 at 08:34
  • Don't Dispose of items when you still need them. – Hans Kesting Nov 11 '15 at 08:35
  • you might want to delay the execution of your sql statement, something like this http://stackoverflow.com/questions/15411393/c-sharp-delay-sql-query-while-user-typing-in-a-textbox – fuchs777 Nov 11 '15 at 08:44

2 Answers2

1

You are clearing the textbox in text changed event. Clearing text will call this event again. Don't use return; statment, the connection is disposed automatically in finally.

lerner1225
  • 862
  • 7
  • 25
1

first of all thanks for the advice and guidance. Finally, I have found the solution to solve this.

Appreciated. :)

the solution as below:

  if (MessageBox.Show("Invalid Item: '" +txt_item.Text+ "', Please re-enter again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
  {
     // Code added
     txt_item.TextChanged -= txt_item_TextChanged;
     txt_item.Clear();
     txt_item.TextChanged += txt_item_TextChanged;
     return;
   }
Empty
  • 123
  • 3
  • 11