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