Here is the simple code to generate bar code and read it while you are dealing in the sale & purchase business.
First You have to add the iTextSharp.dll into the project for this right click on project in solution Explorer and add reference, browse and add it
Add Namespace = using iTextSharp.text.pdf;
Take Two Textboxes whose id item_id and item_code respectively and concate them to generate barcode for this:
- string id = item_id.Value;
- string name = item_code.Text;
-
- string bcode = string.Concat("BCD", id);
-
- Barcode39 bc39 = new Barcode39();
-
- bc39.BarHeight = 100;
- bc39.Code = bcode;
- bc39.X = 45;
-
- System.Drawing.Image bc = bc39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
-
-
-
-
-
- MemoryStream ms = new MemoryStream();
- bc.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
- byte[] a = ms.ToArray();
Now in this we can generate bar code,show barcode on window and store barcode image into Database
After this the next step to read barcode at the time of sale: - For this take a normal textbox and set ontextchanged="code_TextChanged" and AutoPostBack="true" properties.
- Focus on that textbox.
- Read the barcode by the barcode reader.
- You will see the code in the focus textbox.
- By textbox textchange event you can show the details in the correspond to that barcode that machine read.
Suppose, I have a textbox whose id is code and when barcode is read by the machine the ontextchanged event fire:
- protected void code_TextChanged(object sender, EventArgs e)
- {
- string codes = code.Text;
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.CommandText = "Select * from detail where bar_code=@cd";
- cmd.Parameters.AddWithValue("@cd", codes);
- cmd.Connection = cn;
- SqlDataAdapter adp = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- adp.Fill(dt);
- if (dt.Rows.Count > 0)
- {
- name.Text = dt.Rows[0]["name"].ToString();
- des.Text = dt.Rows[0]["des"].ToString();
- price.Text = dt.Rows[0]["price"].ToString();
- }
- else
- {
- name.Text = "";
- des.Text = "";
- price.Text = "";
- }
- }
- }
In this what I do from the code display the name, des and price of the product that read by barcode machine.