Hi All,
I am developing an windows application in .Net 2005 with C# coding. One part of my work, the functionality is
1. Use USB barcode scanner.
2. When something is scanned, it records the item number and automatically pulls the item price from the database.
3. After an item is scanned( In textchanged event), it should automatically add a new product field ( dynamically create multiple textbox) and pulls the data.
4. If i changed the quantity field, it should multiply the price and place into appropriate textbox.
Eg:
ItemNumber Quantity Price
0123455678 1 102
Quantity field should be editable, If i changed into 2, price will be 204.
I struck in the final step, I could not place the the multiplied price in the appropriate textbox. ( You will be clear when check with my code).
Because i am not give the unique id to the dynamically created textbox, So that i cannot place into that. Actually my code is not worked in that.
Below is my code,
public partial class Form1 : Form
{
int x = 8;
int y = 8;
int a = 185;
int k = 560;
int s = 653;
double totalprice = 0;
TextBox txtpdtprice1 = new TextBox();
string pricefrmdb = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TextBox txtbarcode1 = new TextBox();
txtbarcode1.Size = new Size(157, 23);
txtbarcode1.Location = new Point(x, y);
txtbarcode1.Text = "";
txtbarcode1.TextChanged += new EventHandler(this.TextBox_TextChanged);
TextBox txtquantity1 = new TextBox();
txtquantity1.Size = new Size(87, 23);
txtquantity1.Location = new Point(k, y);
txtquantity1.Name = "tquantity1";
txtquantity1.Text = "1";
TextBox txtprice1 = new TextBox();
txtprice1.Size = new Size(87, 23);
txtprice1.Location = new Point(s, y);
txtprice1.Text = "";
txtprice1.Name = "tprice1";
txtpdtprice1 = txtprice1;
pnl1.Controls.Add(txtbarcode1);
pnl1.Controls.Add(txtquantity1);
pnl1.Controls.Add(txtprice1);
txtquantity1.TextChanged += new EventHandler(this.QuantityTextBox_TextChanged);
}
private void TextBox_TextChanged(object sender, System.EventArgs e)
{
TextBox oldBarcodeTextBox = (sender as TextBox);
int item = Convert.ToInt16(oldBarcodeTextBox.Text);
Getdata(item, txtpdtprice1);
findtotal(txtpdtprice1);
TextBox newBarcodeTextBox = new TextBox();
newBarcodeTextBox.Location = new Point(oldBarcodeTextBox.Location.X, oldBarcodeTextBox.Location.Y + oldBarcodeTextBox.Height + 70);
newBarcodeTextBox.Size = newBarcodeTextBox.Size;
newBarcodeTextBox.TextChanged += new EventHandler(TextBox_TextChanged);
TextBox newTextqty = new TextBox();
newTextqty.Size = new Size(87, 23);
newTextqty.Location = new Point(newBarcodeTextBox.Location.X + 553, newBarcodeTextBox.Location.Y);
newTextqty.Text = "1";
newTextqty.TextChanged += new EventHandler(this.QuantityTextBox_TextChanged);
TextBox newTextprice = new TextBox();
newTextprice.Size = new Size(87, 23);
newTextprice.Location = new Point(newBarcodeTextBox.Location.X + 646, newBarcodeTextBox.Location.Y);
newTextprice.Text = "";
txtpdtprice1 = newTextprice;
this.pnl1.Controls.Add(newBarcodeTextBox);
this.pnl1.Controls.Add(newTextqty);
this.pnl1.Controls.Add(newTextprice);
}
private void QuantityTextBox_TextChanged(object sender, System.EventArgs e)
{
TextBox oldQtyTextBox = (sender as TextBox);
int noofqty = Convert.ToInt16(oldQtyTextBox.Text);
if (noofqty > 1)
{
findprice(noofqty, pricefrmdb);
}
}
public void findprice(int qty,string price)
{
double val = Convert.ToDouble(price);
double total;
total = qty * val;
txtpdtprice1.Text = Convert.ToString(total);
}
public void Getdata(int pdtnumber, TextBox price)
{
try
{
string constr = "provider=MicroSoft.jet.OLEDB.4.0;" + "Data Source=...\\db1.mdb;" + "persist Security Info=false";
con.ConnectionString = constr;
con.Open();
string strquery = "SELECT itemnumber, price FROM item where itemnumber = " + pdtnumber + "";
OleDbDataAdapter da = new OleDbDataAdapter(strquery, con);
DataSet ds = new DataSet();
da.Fill(ds, "item");
DataTable dt = ds.Tables["item"];
price.Text = dt.Rows[0][2].ToString();
pricefrmdb = price.Text;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
}
Can anyone give me the solution and also Can give me the suggestion for how to integrate the USB barcode scanner in my application???
Kindly tell me if any fault in this thread too....
Have A Nice Day :)