Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to generate Barcodes with asp.net

Posted on 2012-04-11
15
Medium Priority
?
1,363 Views
Last Modified: 2012-04-23
Greetings mates,

Does anyone know how to generate barcodes?

We are trying to come up with a BarCode for one of our apps.

Management would like a barcode to have customer names (First Name, Last Name) so it can be scanned using one of pitney bowes scanning systems.

I don't mind a link but the link should have *working* examples. I have already scowed the web and each example I tried doesn't work.

Thanks a lot in advance.
0
Comment
Question by:sammySeltzer
15 Comments
 

Assisted Solution

by:koossa
koossa earned 336 total points
ID: 37832915
We're using a .NET control.
http://www.fathsoft.com/bcxnet.html
0
 
LVL 9

Assisted Solution

by:wasiftoor
wasiftoor earned 332 total points
ID: 37832926
Have you tried the following link. Comes with usable source code:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3319&lngWId=10
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 37833027
Thanks for the response guys.

koossa, your link looks great but we would like a freeware. That's not free.

wasiftoor, have you tried that code?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:micropc1
micropc1 earned 332 total points
ID: 37833215
This example uses a free barcode font from IDAutomation....

http://www.codeproject.com/Articles/30322/Barcodes-in-ASP-NET-applications
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 37833318
I saw that. I actually tried to use that.

Now, it works when I hardcode the value that is supposed to be passed from barcode.aspx?code=...

I have 2 questions then.

1, is that supposed to work with the following code?

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" 
   AutoEventWireup="false" Inherits="BarCodes.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" 
        content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:Image id="myBarCode"  runat="server"></asp:Image>
  </form>
 </body>
</HTML>

Open in new window

which is at the bottom of that tutorial page?

Second, I would like the value generated from the db.

My biggest issue with the code is we are running windows application.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 668 total points
ID: 37835319
Just replace this bit

string Code = Request["code"].ToString();

with your code which gets the code from DB.
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 37835678
I actually tried that and it worked. What I have been trying to figure out is:

A, how I do modify the code a bit to generate all the barcodes for each customer.

I know it will be a huge number of records but it is good to do this.

I tried this:

        While dreader.Read()
            FullName = dreader.GetString(0)
            FullName = StrConv(FullName, vbProperCase)
        End While
            dreader.Close()

            '************BarCode Starts***********************
            ' Get the Requested code to be created.
            Dim Code As String = FullName

but kept getting just one record.

B, Perhaps, use search so I can grab one record at a time.

What do you think?
0
 
LVL 1

Accepted Solution

by:
VijayBalkawade earned 332 total points
ID: 37836010
You can make use of free library, http://qrcodenet.codeplex.com/releases/view/73746

Sample code I used is like this,

QrEncoder qrEncoder = new QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.H);
QrCode qrCode = qrEncoder.Encode(string to encode);
//Graphics g = this.CreateGraphics();

Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);

//Save the file on the physical location
string pathToSave = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\BarCodes\\";
if (Directory.Exists(pathToSave))
{
//do nothing
}
else
{
DirectoryInfo di = Directory.CreateDirectory(pathToSave);
}
pathToSave += fields.OrderReqID.ToString() + fields.ShipID + ".png";
renderer.CreateImageFile(qrCode.Matrix, pathToSave, ImageFormat.Png);

Open in new window

0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 37838065
Ok, I am able to get it to work this way:

Markup:
 <body ms_positioning="GridLayout">
  <form id="Form1" method="post" runat="server">
    <input type="hidden" name="code" value="hqcfqs55kioevn55saz0me55" />  
    <asp:Button ID="Button1" runat="server" Text="Generate barcode"></asp:Button> 
</form> 

Open in new window


CodeBehind:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Page.IsPostBack Then
            Dim code As String = Request.Form("code")
            If Not String.IsNullOrEmpty(code) Then
                ' Generate your image here, a code has been specified 
            End If

            ' Get the Requested code to be created.

            ' Multiply the lenght of the code by 40 (just to have enough width)
            Dim w As Integer = code.Length * 40

            ' Create a bitmap object of the width that we calculated and height of 100
            Dim oBitmap As New Bitmap(w, 100)

            ' then create a Graphic object for the bitmap we just created.
            Dim oGraphics As Graphics = Graphics.FromImage(oBitmap)

            ' Now create a Font object for the Barcode Font
            ' (in this case the IDAutomationHC39M) of 18 point size
            Dim oFont As New Font("IDAutomationHC39M", 18)

            ' Let's create the Point and Brushes for the barcode
            Dim oPoint As New PointF(2.0F, 2.0F)
            Dim oBrushWrite As New SolidBrush(Color.Black)
            Dim oBrush As New SolidBrush(Color.White)

            ' Now lets create the actual barcode image
            ' with a rectangle filled with white color
            oGraphics.FillRectangle(oBrush, 0, 0, w, 100)

            ' We have to put prefix and sufix of an asterisk (*),
            ' in order to be a valid barcode
            oGraphics.DrawString("*" + code + "*", oFont, oBrushWrite, oPoint)

            ' Then we send the Graphics with the actual barcode
            Response.ContentType = "image/jpeg"
            oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)

        End If
    End Sub

Open in new window


However, We want the code or in our case, name, to be generated from the db.

How do I modify this code to make it happen?

Thanks,
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37839471
Change

        While dreader.Read()
            FullName = dreader.GetString(0)
            FullName = StrConv(FullName, vbProperCase)
        End While
            dreader.Close()

            '************BarCode Starts***********************
            ' Get the Requested code to be created.
            Dim Code As String = FullName


to


        While dreader.Read()
            FullName = dreader.GetString(0)
            FullName = StrConv(FullName, vbProperCase)
            '************BarCode Starts***********************
            ' Get the Requested code to be created.
            Dim Code As String = FullName
            'Rest of code to generate barcode.
        End While
            dreader.Close()
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 37839501
Sorry, I should have mentioned that I tried that earlier but it was still drawing just one barcode (for one customer), the very first one it sees.

I must be doing something wrong.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37839600
Yes. You are probably using a sql statement which only loads one customer or something else similar. Can you show full code?
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 37839616
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


            Dim connStr As String = ConfigurationManager.ConnectionStrings("dbCust").ConnectionString
            Dim conn As New SqlConnection(connStr)
            Dim myDS As New DataSet
            Dim FullName As String

            Dim sqlStr As String = ""

            sqlStr = "SELECT top 10 fname +' '+ lname AS FullName FROM tblCustomer ORDER BY lname"
            Dim cmd As New SqlCommand(sqlStr, conn)
            Dim myAd As New SqlDataAdapter(cmd)
            'Response.Write(sqlStr)
            'Response.End()
            conn = New SqlConnection(connStr)
            conn.Open()
            cmd = New SqlCommand(sqlStr, conn)

            Dim dreader = cmd.ExecuteReader()


            While dreader.Read()
                FullName = dreader.GetString(0)
                FullName = StrConv(FullName, vbProperCase)


                 ' Get the Requested code to be created.
                Dim Code As String = FullName

                ' Multiply the lenght of the code by 40 (just to have enough width)
                Dim w As Integer = Code.Length * 40

                ' Create a bitmap object of the width that we calculated and height of 100
                Dim oBitmap As New Bitmap(w, 100)

                ' then create a Graphic object for the bitmap we just created.
                Dim oGraphics As Graphics = Graphics.FromImage(oBitmap)

                ' Now create a Font object for the Barcode Font
                ' (in this case the IDAutomationHC39M) of 18 point size
                Dim oFont As New Font("IDAutomationHC39M", 18)

                ' Let's create the Point and Brushes for the barcode
                Dim oPoint As New PointF(2.0F, 2.0F)
                Dim oBrushWrite As New SolidBrush(Color.Black)
                Dim oBrush As New SolidBrush(Color.White)

                ' Now lets create the actual barcode image
                ' with a rectangle filled with white color
                oGraphics.FillRectangle(oBrush, 0, 0, w, 100)

                ' We have to put prefix and sufix of an asterisk (*),
                ' in order to be a valid barcode
                oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint)

                ' Then we send the Graphics with the actual barcode
                Response.ContentType = "image/jpeg"
                oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)

            End While
            dreader.Close()
    End Sub
End Class

Open in new window

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 668 total points
ID: 37839654
Its the way you are writing image to response stream. It will only take one image and return to client. You would need to save files somewhere and point client to those files.
0
 
LVL 29

Author Closing Comment

by:sammySeltzer
ID: 37880999
Thanks all. We decided to abandon this project. I don't know why but thanks for all the help.

Maybe, we can restart later.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question