Generate Barcode And Barcode Reader For Sale and Purchase

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:

  1. string id = item_id.Value;      
  2.           string name = item_code.Text;      
  3.       
  4.           string bcode = string.Concat("BCD", id);      
  5.       
  6.           Barcode39 bc39 = new Barcode39();      
  7.       
  8.           bc39.BarHeight = 100;      
  9.           bc39.Code = bcode;      
  10.           bc39.X = 45;      
  11.       
  12.           System.Drawing.Image bc = bc39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);      
  13.       
  14.        //Response.ContentType = "image/gif";   ----- Show Barcode on window    
  15.                
  16.        //bc.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);  ------Show Barcode on window    
  17.       
  18.           MemoryStream ms = new MemoryStream();                                 
  19.           bc.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);      
  20.           byte[] a = ms.ToArray();                           //   ------- store barcode image into array byte  
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:

  1. For this take a normal textbox and set ontextchanged="code_TextChanged" and AutoPostBack="true" properties.

  2. Focus on that textbox.

  3. Read the barcode by the barcode reader.

  4. You will see the code in the focus textbox.

  5. 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:

  1. protected void code_TextChanged(object sender, EventArgs e)    
  2.     {    
  3.         string codes = code.Text;    
  4.         using (SqlCommand cmd = new SqlCommand())    
  5.         {    
  6.             cmd.CommandText = "Select * from detail where bar_code=@cd";    
  7.             cmd.Parameters.AddWithValue("@cd", codes);    
  8.             cmd.Connection = cn;    
  9.             SqlDataAdapter adp = new SqlDataAdapter(cmd);    
  10.             DataTable dt = new DataTable();    
  11.             adp.Fill(dt);    
  12.             if (dt.Rows.Count > 0)    
  13.             {    
  14.                 name.Text = dt.Rows[0]["name"].ToString();    
  15.                 des.Text = dt.Rows[0]["des"].ToString();    
  16.                 price.Text = dt.Rows[0]["price"].ToString();    
  17.             }    
  18.             else    
  19.             {    
  20.                 name.Text = "";    
  21.                 des.Text = "";    
  22.                 price.Text = "";    
  23.             }    
  24.         }    
  25.     }  
In this what I do from the code display the name, des and price of the product that read by barcode machine.