Israel

Israel

  • 997
  • 1.3k
  • 154.8k

trying to read code from my textbox... Doesnt

Sep 5 2014 11:32 PM
Hi!

I wrote these to try to read the data that appear on the textbox when the barcode reader is  pointed on a barcode book for example. But its doesnt. Anyone can explain please.

private void codigoBarraConsStocks_KeyPress(object sender, KeyPressEventArgs e)
        {
            string connectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=master;Integrated Security=True";
            SqlConnection sqlCon = new SqlConnection(connectionString);
            sqlCon.Open();
            string commandString = "select * from consultoria where codigobarra ='" + codigoBarraConsStocks.Text + "'";
            SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
            SqlDataReader read = sqlCmd.ExecuteReader();

            if (read.HasRows)
            {
                while (read.Read())
                {
                    codigoBarraStocks.Text = read["codigobarra"].ToString(); // it will show the code
                    produtoStocks.Text = read["produto"].ToString(); // it will show the code
                    precoStocks.Text = read["preco"].ToString(); // it will show the code
                    nStocksStocks.Text = read["n_stocks"].ToString(); // it will show the code 
                }
            }
            else
            {
                MessageBox.Show("A record with a code of " + codigoBarraConsStocks.Text + " was not found");
            }

            read.Close();
            sqlCon.Close();
        }




Answers (7)