Israel

Israel

  • NA
  • 847
  • 107.8k

Why takes so long to read data?

Oct 14 2014 8:50 PM
Hi!
 
I have a pertinent question. Maybe will help in this forum (for those who have this problem).
I write code to read data from my database. What I did: I put on my form one textbox, two labels (label1 and label2).
Then when I focus my barecode on my object (for example a CD cover with barcode). Its appear naturally on my textbox.
After that its clean my textbox and push the code on label1... then the labe2 show me the name of the product. Until there its work well.
But there is a very, very big problem:
when appear the code on the textbox. This textbox should be quickly be cleaned and this code should appear on the label1. But before to appear on the label its take 5 seconds. What happen??? Very strange the behavour!!! Why so long time??? My computer have good and strong memory!
Please anyone can help??? 
 
My code:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string lastScannedCode = "";
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
lastScannedCode = textBox1.Text;
label1.Text = lastScannedCode;
textBox1.Clear();
}
}
void ShowTheLastScannedCode()
{
MessageBox.Show(lastScannedCode);
}
private void label1_TextChanged(object sender, EventArgs e)
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TestBarcodeReader\WindowsFormsApplication1\WindowsFormsApplication1\App_Data\office.mdb;Persist Security Info=False";
OleDbConnection sqlCon = new OleDbConnection(connectionString);
sqlCon.Open();
string commandString = "select * from barcode where code ='" + label1.Text + "'";
OleDbCommand sqlCmd = new OleDbCommand(commandString, sqlCon);
OleDbDataReader read = sqlCmd.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
label2.Text = read["products"].ToString(); // it will show the code
}
}
else
{
MessageBox.Show("A record with a code of " + label1.Text + " was not found");
}
read.Close();
sqlCon.Close();
}
}
}
 

Answers (2)