Hi @RAVI,
You can use QRCoder, an open source library QR code generator, by Manage Nuget Packages download.
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);
}
}
}
}
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.