Sandeep Kumar

Sandeep Kumar

  • 1.1k
  • 684
  • 61.7k

How take print of each QR Code

Dec 27 2022 5:06 AM

I want to print  QR Code,Which i want QR Code

My Aspx.Page

<asp:GridView ID="grdQrCodeResult" runat="server" AutoGenerateColumns="false" OnRowDataBound="grdQrCodeResult_RowDataBound" OnSelectedIndexChanged="grdQrCodeResult_SelectedIndexChanged">
    <Columns>
         <asp:TemplateField HeaderText="Port" HeaderStyle-HorizontalAlign="Center">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                 <asp:Label ID="lblPortName" runat="server" Text='<%#Eval("Port") %>' SkinID="GridLabel1"></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Lots#" HeaderStyle-HorizontalAlign="Center">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                 <asp:Label ID="lblHatchName" runat="server" Text='<%#Eval("Lots") %>' SkinID="GridLabel1"></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="WMT">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                 <asp:Label ID="lblHatch1" runat="server" Text='<%#Eval("WMT") %>' SkinID="GridLabel1"></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="QR Code">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                <asp:PlaceHolder ID="plBarCode" runat="server" />
             </ItemTemplate>
         </asp:TemplateField>
                  
         <asp:TemplateField HeaderText="QR Code1">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                <asp:PlaceHolder ID="plBarCodes" runat="server" />

             <%--    <asp:ImageButton runat="server" ID="plBarCodes" OnClick="plBarCodes_Click" />--%>
             </ItemTemplate>
         </asp:TemplateField>

    </Columns>
</asp:GridView>

CS Page

using QRCoder;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Reports_QRCode : System.Web.UI.Page
{
    SqlConnection con;
    int PageNum = 1;
    String strConn = ConfigurationManager.ConnectionStrings["ConOrms"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowData(1);
        }

    }
    protected void ShowData(int PageNum)
    {
        int TotalRecord = 0;
        DataTable dt = new DataTable();
         //con  = new SqlConnection(strConn);
        DataTable dtQrReport = new DataTable(); //WR_TRF.GetTRFReport(BranchID, FromDate, ToDate, Convert.ToInt16(SessionManager.Instance.RoleId),txtTRFno.Text.Trim());

        try
        {
            using (SqlConnection con = new SqlConnection(strConn))
            {
                using (SqlCommand cmd = new SqlCommand("Sp_GetLotDetails", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                
                    cmd.Parameters.Add("@PageNumber", SqlDbType.Int).Value = PageNum;
                    cmd.Parameters.Add("@PageSize", SqlDbType.Int).Value = 10;
                    cmd.CommandTimeout = 999999;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);
                    adp.Fill(dtQrReport);
                }
            }
        }
        catch (Exception ex)
        {

        }
        DataTable distinctParameter = new DataTable();
        DataView viewSampleLlst = new DataView(dtQrReport);
        if (dtQrReport.Rows.Count > 0)
        {
            distinctParameter = viewSampleLlst.ToTable(true, "TotalCount");
            TotalRecord = Convert.ToInt16(distinctParameter.Rows[0]["TotalCount"]);
            grdQrCodeResult.DataSource = dtQrReport;
            grdQrCodeResult.DataBind();
            gvPager.Visible = (dtQrReport.DefaultView.Count > 0);
            gvPager.PageSize = 10;
            gvPager.TotalRecord = TotalRecord;
            gvPager.CurrentPage = PageNum;
            hidPageNo.Value = PageNum.ToString();
            gvPager.DataLoad();
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "Key", "<script>MakeStaticHeader('" + grdQrCodeResult.ClientID + "', 300, 750 , 41 ,true); </script>", false);
        }
        else
        {
            gvPager.Visible = false;
            grdQrCodeResult.DataSource = null;
            grdQrCodeResult.DataBind();
        }
        //SqlDataAdapter adapt = new SqlDataAdapter("select   distinct P.Port,Lots,WMT from [IQMS].[dbo].[tblDischargeLots] t LEFT JOIN [IQMS].[dbo].tblPort P on t.PortId=P.PortId where WMT >20000", con);
        //con.Open();
        //adapt.Fill(dt);
        //con.Close();
        //grdQrCodeResult.DataSource = dt;
        //grdQrCodeResult.DataBind();
    }

    protected void grdQrCodeResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Checking the RowType of the Row  
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grdQrCodeResult, "Select$" + e.Row.RowIndex);
            e.Row.Cells[3].Attributes["style"] = "cursor:pointer";
            e.Row.Cells[4].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grdQrCodeResult, "Select$" + e.Row.RowIndex);
            e.Row.Cells[4].Attributes["style"] = "cursor:pointer";
            //Find the PlaceHolder control.
            PlaceHolder plBarcode = (e.Row.FindControl("plBarCode") as PlaceHolder);
            PlaceHolder plBarcodes = (e.Row.FindControl("plBarCodes") as PlaceHolder);
            int index = e.Row.RowIndex;
            string code = "Scanner" + Convert.ToInt32(index);
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            //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 = 60;
            imgBarCode.Width = 60;
            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);
                }
                plBarcode.Controls.Add(imgBarCode);
               
            }
            System.Web.UI.WebControls.Image imgBarCodes = new System.Web.UI.WebControls.Image();
            imgBarCodes.Height = 60;
            imgBarCodes.Width = 60;
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
                    imgBarCodes.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
             
                plBarcodes.Controls.Add(imgBarCodes);
            }
        }
    }

    protected void grdQrCodeResult_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string message = "Cell Click";
        //ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
        //ShowData(1);
       
    }

    protected void gvPager_Paging_Click(object sender, CommandEventArgs e)
    {
        string CurrentPage = e.CommandArgument.ToString();
        PageNum = Convert.ToInt32(CurrentPage);
        ViewState["PageNumDraft"] = PageNum;
        ShowData(Convert.ToInt32(ViewState["PageNumDraft"]));
    }
}

My Screen


Answers (8)