Update column value when Click on Print Button using jQuery Ajax in ASP.Net

indradeo
 
on Aug 04, 2022 06:38 AM
Sample_197428.zip
913 Views

how to update "emp_in_flag" while click on print button

namespace vstr_gt_ps_systm
{
    public partial class cisf : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {
                this.BindGrid();
            }
            if (Session["emp_nm"] == null)
                Response.Redirect("emp.aspx");
            else
            {
                String EmplooyeId = Session["emp_id"].ToString();
                Label1.Text = " " + EmplooyeId + "";
                String emp_nm = Session["emp_nm"].ToString();
                Label2.Text = "" + emp_nm + " ";
            }
            Label1.Visible = false;
            

        }
        protected void Search(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        private void BindGrid()
        {
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT Id, emp_id,dept, emp_nm,vst_nm,vst_adhr,fa_hu_nm,gndr,crnt_add,pur_pos,emp_in_flag,cisf_in_flag,mrk_in_emp,mrk_in_cisf,cisf_out_flag,mrk_out_emp,mark_out_cisf FROM vstr WHERE emp_id LIKE '%' + @emp_id + '%' and dept LIKE '%' + @dept + '%'and cisf_out_flag='N'";
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@emp_id", txtSearch.Text.Trim());
                    cmd.Parameters.AddWithValue("@dept", txtSearch0.Text.Trim());
                    DataTable dt = new DataTable();
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }
        protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.BindGrid();
        }
        protected void OnSendSession(object sender, EventArgs e)
        {

            Response.Redirect("~/Default.aspx");
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditButton")
            {
               
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView1.Rows[index];
                Response.Redirect("Default.aspx?Id=" + row.Cells[0].Text);
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[10].Text == "YES")
                {
                    (e.Row.FindControl("LinkButton1") as LinkButton).Visible = false;

                }

            }

        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {

        }

        private static byte[] ConvertHexToBytes(string hex)
        {
            byte[] bytes = new byte[hex.Length / 2];
            for (int i = 0; i < hex.Length; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }

        [WebMethod(EnableSession = true)]
        public static string GetCapturedImage()
        {
            string url = HttpContext.Current.Session["CapturedImage"].ToString();
            HttpContext.Current.Session["CapturedImage"] = null;
            return url;
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("lgn.aspx");
        }
        protected void Button3_Click(object sender, EventArgs e)
        {

        }
        protected void Closed(object sender, EventArgs e)
        {

            GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
            int Id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32((sender as Button).CommandArgument)].Values[0]);
            string cisf_out_flag = "YES";
            string mark_out_cisf = DateTime.Now.ToString();
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("UPDATE vstr SET cisf_out_flag = @cisf_out_flag,mark_out_cisf=@mark_out_cisf WHERE Id = @Id"))
                {

                    cmd.Parameters.AddWithValue("@Id", Id);
                    cmd.Parameters.AddWithValue("@cisf_out_flag", cisf_out_flag);
                    cmd.Parameters.AddWithValue("@mark_out_cisf", mark_out_cisf);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    GridView1.EditIndex = -1;
                    row.Visible = false;
                }
            }
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            Response.Redirect("cisf_gt-crtn.aspx?emp=" + Label2.Text);
           
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
PrinceG
 
on Aug 19, 2022 05:54 AM

Hi indradeo

Please refer below sample.

HTML

<table border="1" class="auto-style43">
    <tr>
        <td class="auto-style75">
            <img src="images/m1.png" alt="Capture" class="auto-style42" />
        </td>
        <th class="auto-style46" style="box-sizing: border-box;" colspan="4">
            <span class="auto-style44">Meja Thermal Power Plant<br />
                <span style="color: rgb(0, 0, 0); font-family: Roboto; font-size: 15px; font-variant-ligatures: normal;
                    font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: center;
                    text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;
                    -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial;
                    text-decoration-style: initial; text-decoration-color: initial; display: inline !important;
                    float: none;">A Joint Venture of NTPC Ltd &amp; UPRVUNL</span></span><br class="auto-style44" />
        </th>
    </tr>
    <tr>
        <td class="auto-style100">
        </td>
        <th class="auto-style101" style="box-sizing: border-box;" colspan="4">
            <font style="box-sizing: border-box;"><span class="auto-style45">VISITOR GATE PASS </span>
            </font>
        </th>
    </tr>
    <tr>
        <td class="auto-style63" rowspan="2">
            <img id="imgCapture" src="images/m1.png" alt="Capture" />
        </td>
        <th class="auto-style68" style="box-sizing: border-box;" colspan="3">
            Visitor Id:
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </th>
        <th class="auto-style88">
            Visitor Aadhar No. :
            <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
        </th>
    </tr>
    <tr>
        <th class="auto-style83" style="box-sizing: border-box;" colspan="3">
            Visitor Name :
            <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
        </th>
        <th class="auto-style84">
            Gender :
            <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
        </th>
    </tr>
    <tr>
        <th class="auto-style98" style="box-sizing: border-box;">
            Father/Husband Name:
        </th>
        <th class="auto-style114" style="box-sizing: border-box;">
            &nbsp;<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
        </th>
        <th class="auto-style96" colspan="2">
            Current Address :&nbsp;
        </th>
        <th class="auto-style97">
            <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
        </th>
    </tr>
    <tr>
        <th class="auto-style94" style="box-sizing: border-box;">
            Purpose of visit :
        </th>
        <th class="auto-style54" style="box-sizing: border-box;" colspan="3">
            <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
        </th>
        <th class="auto-style72">
        </th>
    </tr>
    <tr>
        <th class="auto-style91" style="box-sizing: border-box;">
            Emp.Name:
        </th>
        <th class="auto-style108" style="box-sizing: border-box;">
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        </th>
        <th class="auto-style103">
            Emp. ID :
        </th>
        <th class="auto-style113">
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
        </th>
        <th class="auto-style93">
            Department :<asp:Label ID="Label12" runat="server" Text=""></asp:Label>
        </th>
    </tr>
    <tr>
        <th class="auto-style99">
            Gate pass Cretion Date/Time:
        </th>
        <th class="auto-style117" colspan="3">
            <asp:Label ID="Label10" runat="server" Text="Label" CssClass="auto-style106"></asp:Label>
        </th>
        <th class="auto-style116">
        </th>
    </tr>
    <tr>
        <th height="120" colspan="2" class="auto-style73" style="height: 60px">
            CISF Signature
        </th>
        <th height="120" colspan="3" class="auto-style73" style="height: 60px">
            Employee Signature
        </th>
    </tr>
    <tr>
        <th colspan="5" class="auto-style73">
            <div class="noprint">
                <input id="btnCapture" type="button" value="Image Capture" />
                &nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="btnPrint" runat="server" Text="Print" Height="27px" Width="53px"
                    OnClientClick="return Print();" />
                <asp:Button ID="Button1" runat="server" Text="Back" CssClass="auto-style81" CausesValidation="false"
                    Height="28px" OnClick="Button1_Click" Width="81px" />
                &nbsp;&nbsp;&nbsp;&nbsp;
            </div>
        </th>
    </tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function Print() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/PrintName",
            data: "{name :' " + $('[id*=Label1]').html() + " ', customerId:' " + $('[id*=Label3]').html() + " '}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert(response.d);
                window.print();
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
        return false;
    };
</script>

Namespaces

C#

using System.IO;
using System.Data;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.IO
Imports System.Data
Imports System.Web.Services
Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

int customer = 0;
protected void Page_Load(object sender, EventArgs e)
{
    //customer = Convert.ToInt32(Request.QueryString["CustomerId"].ToString());
    Session["EmpId"] = customer;
    if (!IsPostBack)
    {
        BindTextBoxvalues();
    }
}

[WebMethod]
public static string PrintName(string name, string customerId)
{
    string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name = @Name WHERE CustomerId=@CustomerId", con))
        {
            cmd.Parameters.AddWithValue("@Name", name.Trim());
            cmd.Parameters.AddWithValue("@CustomerId", customerId.Trim());
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    return "Thanks for Using Visitor Gate Pass System. Please Click OK !!!!";
}

private void BindTextBoxvalues()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("select * from Customers where CustomerId=" + customer, con);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
}

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("cisf.aspx");
}

VB.Net

Private customer As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'customer = Convert.ToInt32(Request.QueryString("CustomerId").ToString())
    Session("EmpId") = customer

    If Not IsPostBack Then
        BindTextBoxvalues()
    End If
End Sub

<WebMethod()>
Public Shared Function PrintName(ByVal name As String, ByVal customerId As String) As String
    Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constring)
        Using cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name = @Name WHERE CustomerId=@Id", con)
            cmd.Parameters.AddWithValue("@Name", name.Trim())
            cmd.Parameters.AddWithValue("@Id", customerId.Trim())
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Using
    End Using

    Return "Thanks for Using Visitor Gate Pass System. Please Click OK !!!!"
End Function

Private Sub BindTextBoxvalues()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    Dim cmd As SqlCommand = New SqlCommand("select * from Customers where CustomerId=" & customer, con)
    Dim dt As DataTable = New DataTable()
    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
    da.Fill(dt)
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("cisf.aspx")
    
End Sub

Screenshot