Generate QR code Image and save in Database using C# and VB.net in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

item id is 10014589

now i  want to gernate qr code for this id and save into Sql database 

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi SajidHussa,

Refer below sample.

I created below sample by taking reference of below link -

Dynamically generate and display QR code Image in ASP.Net

HTML

<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" />
<hr />
<br />
<asp:GridView runat="server" ID="gvQRCode" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Image">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" Height="50px" Width="75px" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using QRCoder;
using System.IO;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

VB.Net

Imports QRCoder
Imports System.IO
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void btnGenerate_Click(object sender, EventArgs e)
{
    string code = txtCode.Text;
    QRCodeGenerator qrGenerator = new QRCodeGenerator();
    QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
    System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
    imgBarCode.Height = 150;
    imgBarCode.Width = 150;
    using (Bitmap bitMap = qrCode.GetGraphic(20))
    {
        using (MemoryStream ms = new MemoryStream())
        {
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteImage = ms.ToArray();
            imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
            SqlCommand cmd = new SqlCommand("INSERT INTO tblQRCode(Data) VALUES(@Data)", con);

            cmd.Parameters.AddWithValue("@Data", byteImage);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            cmd = new SqlCommand("SELECT Data FROM tblQRCode", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gvQRCode.DataSource = dt;
            gvQRCode.DataBind();
        }
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView dr = (DataRowView)e.Row.DataItem;
        string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
        (e.Row.FindControl("Image1") as System.Web.UI.WebControls.Image).ImageUrl = imageUrl;
    }
}

VB.Net

Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim code As String = txtCode.Text
    Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
    Dim qrCode As QRCodeGenerator.QRCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q)
    Dim imgBarCode As System.Web.UI.WebControls.Image = New System.Web.UI.WebControls.Image()
    imgBarCode.Height = 150
    imgBarCode.Width = 150

    Using bitMap As Bitmap = qrCode.GetGraphic(20)

        Using ms As MemoryStream = New MemoryStream()
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
            Dim byteImage As Byte() = ms.ToArray()
            imgBarCode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(byteImage)
            Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
            Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblQRCode(Data) VALUES(@Data)", con)
            cmd.Parameters.AddWithValue("@Data", byteImage)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            cmd = New SqlCommand("SELECT Data FROM tblQRCode", con)
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim dt As DataTable = New DataTable()
            da.Fill(dt)
            gvQRCode.DataSource = dt
            gvQRCode.DataBind()
        End Using
    End Using
End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim dr As DataRowView = CType(e.Row.DataItem, DataRowView)
        Dim imageUrl As String = "data:image/jpg;base64," & Convert.ToBase64String(CType(dr("Data"), Byte()))
        TryCast(e.Row.FindControl("Image1"), System.Web.UI.WebControls.Image).ImageUrl = imageUrl
    End If
End Sub

Screenshot