Sandeep Kumar

Sandeep Kumar

  • 1k
  • 627
  • 50.3k

how to take directly print of paricular QR code

Jan 17 2023 11:32 AM

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

   
    protected void ShowData(int PageNum)
    {
        int WOID = Convert.ToInt32(ViewState["WoidForBindData"]);
        DataSet dsSmplPrep = new DataSet(); //WR_TRF.GetTRFReport(BranchID, FromDate, ToDate, Convert.ToInt16(SessionManager.Instance.RoleId),txtTRFno.Text.Trim());
        DataTable dtSmplPrep = new DataTable();
        try
        {
            using (SqlConnection con = new SqlConnection(strConn))
            {
                using (SqlCommand cmd = new SqlCommand("VVS_SAMPLE_PREPARARION", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WOID", SqlDbType.Int).Value = WOID;
                    cmd.CommandTimeout = 999999;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);
                    adp.Fill(dtSmplPrep);
                }
            }
        }
        catch (Exception ex)
        {

        } 
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        if (dtSmplPrep != null)
        {
            if (dtSmplPrep.Rows.Count > 0)
            {
                dt = dtSmplPrep;
                // DataRow dr = null;
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {

                            BoundField boundField = new BoundField();
                            boundField.DataField = dt.Columns[i].ColumnName.ToString();
                            boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
                            grdsampleprep.Columns.Add(boundField);
                            //  dt1.Columns.Add(new DataColumn(dt.Columns[i].ColumnName, typeof(string)));

                        }
                      
                    }
                }
            }
        }
        //PlaceHolder1.Controls.Add(gv);
        BindGridView(grdsampleprep, dt);
    }


    static DataTable ListToDataTable<T>(IEnumerable<T> list)
    {
        var dt = new DataTable();
        foreach (var info in typeof(T).GetProperties())
        {
            dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
        }
        foreach (var t in list)
        {
            var row = dt.NewRow();
            foreach (var info in typeof(T).GetProperties())
            {
                row[info.Name] = info.GetValue(t, null);
            }
            dt.Rows.Add(row);
        }
        return dt;
    }
    private void BindGridView(GridView gv, DataTable dt)
    {
        gv.DataSource = dt;
        gv.DataBind();
    }

    protected void gvPager_Paging_Click(object sender, CommandEventArgs e)
    {

    }

    #region AutoComplete Work order No
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> SearchWO(string prefixText, int count)
    {
        List<string> customers = new List<string>();
        string Item = string.Empty;
        try
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConOrms"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "GetWoIdBySearchText";
                    cmd.Parameters.Add("@SearchText", SqlDbType.NVarChar).Value = prefixText;
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            Item = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(sdr["JobOrderNo"].ToString(), sdr["WOID"].ToString());
                            customers.Add(Item);
                        }
                    }
                    con.Close();

                }
            }
        }
        catch (Exception ex)
        {
        }
        return customers;
    }
    #endregion

    protected void txtWorkOrderNo_TextChanged(object sender, EventArgs e)
    {
        int Woid = 0;
        //string WONO= txtTextingWO.Text;
        //GetEditWorkOrder(GetWorkOrderID(WONO));
        if (txtWorkOrderNo.Text != string.Empty)
        {
            try
            {
                string[] FindValue = txtWorkOrderNo.Text.Trim().Split(';');
                if (FindValue.Length > 1)
                {
                    Woid = Convert.ToInt32(FindValue[1].ToString());
                    ViewState["WoidForBindData"] = Woid;
                }
                txtWorkOrderNo.Text = FindValue[0].ToString();
            }
            catch (Exception ex) { }

        }
    }

    protected void btnInspectionCodeTagging_Click(object sender, EventArgs e)
    {
        ShowData(1);
    }

    protected void grdsampleprep_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int index = e.Row.RowIndex;
            int indexcell = e.Row.Cells.Count - 1;

            GridView sendingGridView = (GridView)sender;
            for (int j = 4; j <= indexcell; j++)
            {
                PlaceHolder pl = new PlaceHolder();
                pl.ID = e.Row.Cells[j].Text;
               
                if (e.Row.Cells[j].Text != "&nbsp;")
                {
                    //e.Row.Cells[j].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.gv, "Select$" + e.Row.RowIndex);
                    //e.Row.Cells[j].Attributes["style"] = "cursor:pointer";
                    string code = e.Row.Cells[j].Text;
                    QRCodeGenerator qrGenerator = new QRCodeGenerator();
                    QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
                    QRCode qrCode = new QRCode(qrCodeData);
                    System.Web.UI.WebControls.ImageButton imgBarCode = new System.Web.UI.WebControls.ImageButton();
                    imgBarCode.ID = "imgBarCode" + j;
                    imgBarCode.Click += OnLinkClick;
                   
                    ImageButton lb3 = e.Row.FindControl(imgBarCode.ID) as ImageButton;
                    if (lb3 != null)
                        ScriptManager.GetCurrent(this).RegisterPostBackControl(lb3);
                    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);
                            //imgBarCode.OnClientClick="return PrintPanel();";
                            //imgBarCode.PostBackUrl = "QRCodePrint.aspx?index=" + index + "&code=" + code.ToString();
                            //imgBarCode.Attributes.Add("onclick", "javascript:PrintPanel("+ byteImage + "); return false;");
                        }
                        pl.Controls.Add(imgBarCode);
                      
                    }
                    //e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackClientHyperlink((GridView)sender, "Select$" + e.Row.RowIndex));
                }
                e.Row.Cells[j].Controls.Add(pl);
            }
        }
    }
 
 
    protected void PopupClose1_Click(object sender, ImageClickEventArgs e)
    {
       // ShowData(1);
        modalBackground.Attributes.Add("style", "display:none");
        divPrint.Attributes.Add("style", "display:none");
    }
 
    protected void grdsampleprep_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int index = e.Row.RowIndex;
            int indexcell = e.Row.Cells.Count - 1;

            GridView sendingGridView = (GridView)sender;
            for (int j = 4; j <= indexcell; j++)
            {
                PlaceHolder pl = new PlaceHolder();
                pl.ID = e.Row.Cells[j].Text;
           
                if (e.Row.Cells[j].Text != "&nbsp;")
                { 
                    string code = e.Row.Cells[j].Text;
                    QRCodeGenerator qrGenerator = new QRCodeGenerator();
                    QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
                    QRCode qrCode = new QRCode(qrCodeData);
                    System.Web.UI.WebControls.ImageButton imgBarCode = new System.Web.UI.WebControls.ImageButton();
                    imgBarCode.ID = "imgBarCode" + j;
                    imgBarCode.CommandArgument = code;
                    imgBarCode.Click += OnLinkClick;
                
                    ImageButton lb3 = e.Row.FindControl(imgBarCode.ID) as ImageButton;
                    if (lb3 != null)
                        ScriptManager.GetCurrent(this).RegisterPostBackControl(lb3);
                    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);
                            //imgBarCode.Attributes.Add("onclick", "javascript:PrintPanel(" + byteImage + "); return false;");
                            
                        }
                        pl.Controls.Add(imgBarCode);

                    } 
                }
                else
                {
                  string code=  e.Row.Cells[j].Text;
                }
                e.Row.Cells[j].Controls.Add(pl);
            }
        }
    }
    protected void OnLinkClick(object sender, EventArgs e)
    {
        ImageButton lb = (ImageButton)sender;
        GridViewRow row = (GridViewRow)lb.NamingContainer;
        string code = lb.CommandArgument;
        if (row != null)
        {
            modalBackground.Attributes.Add("style", "display:block");
            divPrint.Attributes.Add("style", "display:block");
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            imgBarCode.Height = 200;
            imgBarCode.Width = 200;
            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);
            }
        }

----------------My UI


Answers (1)