Change Label Data In to QR code For Free

RAVI 1,076 Reputation points
2024-04-01T14:50:13.3766667+00:00

Hello

This is my gridview data

<asp:TemplateField HeaderText="NAME"> 
<ItemTemplate>
 <asp:Label ID="PN" runat="server" ForeColor="Black" Text='<%# Bind("process_name") %>'></asp:Label> 
</ItemTemplate>
<HeaderStyle ForeColor="Teal"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

I need to convert this data into QR code and it should be free license for commercial use.

Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-04-02T03:15:27.34+00:00

    Hi @RAVI,

    You can use QRCoder, an open source library QR code generator, by Manage Nuget Packages download.

    User's image

    I wrote an example based on your code for your reference.

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                  <Columns>
                      <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="Customer ID" />
                      <asp:TemplateField HeaderText="NAME">
                          <ItemTemplate>
                              <asp:Label ID="PN" runat="server" ForeColor="Black" Text='<%# Bind("Name") %>'></asp:Label>
                              <asp:Image ID="QRImage1" runat="server" Width="50" Height="50" Visible="false" />
                          </ItemTemplate>
                          <HeaderStyle ForeColor="Teal"></HeaderStyle>
                          <ItemStyle HorizontalAlign="Center" />
                      </asp:TemplateField>
                  </Columns>
              </asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindGrid();
        }
    }
    private void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name FROM Customers"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {           
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbName = (e.Row.FindControl("PN") as Label);
            System.Web.UI.WebControls.Image image = (e.Row.FindControl("QRImage1") as System.Web.UI.WebControls.Image);
            lbName.Visible = false;
            image.Visible = true;
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(lbName.Text, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData);              
            using (Bitmap bitMap = qrCode.GetGraphic(10))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
                    image.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
            }
        }
    }
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-04-01T16:31:03.56+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.