19 Feb 2014

Signature capture from signature pad in Web Application asp.net using c# (or) web signature capture asp net using mouse asp.net

In this post I Explained about capture the signature from signature pad to connect the Web application.  You have to use web signuature pad for this application.


.aspx code

<form id="form1" runat="server">
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <div id="wPaint" style="position:relative; width:1200px; height:500px; background:#ACACAC; border:solid black 1px; overflow:hidden;"></div>
        <asp:Button ID="BtnSign" runat="server" Text="Save" CssClass="button" OnClientClick="javascript:saveImage();" onclick="BtnSign_Click" />
        <asp:Button ID="BtnClearSign" runat="server" Text="Clear" CssClass="button" OnClientClick="javascript:loadImage();" />
       
        <script type="text/javascript">
                $("#wPaint").wPaint({
                      image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAECAQGDHihRBAIECBAgYND9AAECBAgQCAgY9ECJIhAgQIAAAYPuBwgQIECAQEDAoAdKFIEAAQIECBh0P0CAAAECBAICBj1QoggECBAgQMCg+wECBAgQIBAQMOiBEkUgQIAAAQIG3Q8QIECAAIGAgEEPlCgCAQIECBAw6H6AAAECBAgEBAx6oEQRCBAgQICAQfcDBAgQIEAgIGDQAyWKQIAAAQIEDLofIECAAAECAQGDHihRBAIECBAgYND9AAECBAgQCAgY9ECJIhAgQIAAgQMI/gEtiL4PDAAAAABJRU5ErkJggg==",
                      drawDown: function(e, element){ $("#canvasDown").val(element.settings.mode +": " + e.pageX + ',' + e.pageY); },
                      drawMove: function(e, element){ $("#canvasMove").val(element.settings.mode +": " + e.pageX + ',' + e.pageY); },
                      drawUp: function(e, element){ $("#canvasUp").val(element.settings.mode +": " + e.pageX + ',' + e.pageY); }
                });
                function loadImage()
                {
                      $("#wPaint").wPaint("image", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQCBj1QoggECBAgQMCg+wECBAgQIBAQMOiBEkUgQIAAAQIG3Q8QIECAAIGAgEEPlCgCAQIECBAw6H6AAAECBAgEBAx6oEQRCBAgQICAQfcDBAgQIEAgIGDQAyWKQIAAAQIEDLofIECAAAECAQGDHihRBAIECBAgYND9AAECBAgQCAgY9ECJIhAgQIAAgQMI/gEtiL4PDAAAAABJRU5ErkJggg==");
                }
                function saveImage()
                {
                      var imageSignData = $("#wPaint").wPaint("image");
                    document.getElementById('<%= HiddenField1.ClientID %>').value = imageSignData;
                }
          </script>                                
    </form>

.cs page code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Drawing.Imaging;

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

        }

        protected void BtnSign_Click(object sender, EventArgs e)
        {
            try
            {
                string PATH = Server.MapPath(ResolveUrl("~\\"));
                string strPhoto = HiddenField1.Value; //Get the image from flash file
                int l = strPhoto.Length;
                strPhoto = strPhoto.Substring(22, l - 22);
                byte[] photo = Convert.FromBase64String(strPhoto);
                FileStream fs = new FileStream(PATH + "Sign.png", FileMode.OpenOrCreate, FileAccess.Write);
                BinaryWriter br = new BinaryWriter(fs);
                br.Write(photo);
                br.Flush();
                br.Close();
                fs.Close();

                string script2 = "alert('Signature Saved..');";
                System.Web.HttpContext.Current.Response.Write("<script language=\"javascript\">" + script2 + "</script>");

                InvertImage();
            }
            catch (Exception Ex)
            {
                string script3 = "alert('" + Ex + "');";
                System.Web.HttpContext.Current.Response.Write("<script language=\"javascript\">" + script3 + "</script>");
            }
        }

        protected void InvertImage()
        {
            Bitmap bimg = (Bitmap)System.Drawing.Image.FromFile(Server.MapPath("~\\Sign.png"));
            Bitmap img = null;
            img = new Bitmap(bimg.Width, bimg.Height);
            for (int x = 0; x < bimg.Width; x++)
            {
                for (int y = 0; y < bimg.Height; y++)
                {
                    //Get the color
                    Color clr = bimg.GetPixel(x, y);
                    //Invert the clr
                    clr = Color.FromArgb(255 - clr.R, 255 - clr.G, 255 - clr.B);
                    //Update the color
                    img.SetPixel(x, y, clr);
                }
            }
            bimg.Dispose();
            img.Save(Server.MapPath("~\\Signature.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}


print gridview data in asp.net using c# (or) export gridview to word/excel/pdf/csv in asp.net

In this post I Explained about exporting gridview data to the word or excel format. You can down load the complete grid view data on clicking the image button in the figure.




.Aspx Code :

  <form id="form1" runat="server">
    <div>&nbsp&nbsp&nbsp&nbsp
        <asp:ImageButton ID="ImgBtnBack" runat="server" ImageUrl="~/images/back.png" OnClick="ImgBtnBack_Click" />
        <asp:ImageButton ID="ImgBtnPrint" runat="server" ImageUrl="~/images/print01.png" /> &nbsp&nbsp&nbsp&nbsp
        <asp:ImageButton ID="ImgBtnWord" runat="server" ImageUrl="~/images/Word01.png" OnClick="ImgBtnWord_Click" /> &nbsp&nbsp&nbsp&nbsp
        <asp:ImageButton ID="ImgBtnExcel" runat="server" ImageUrl="~/images/Excel01.png" OnClick="ImgBtnExcel_Click" />
    </div>
    <div id="VQGridView">
        <asp:GridView ID="GridViewResult" runat="server" HorizontalAlign="Center"
            GridLines="None" Width="200%"
            CssClass="pnl-div" EmptyDataText="No Records Avaialable.."  >
            <FooterStyle BackColor="" Font-Bold="true" ForeColor="" />
            <RowStyle BackColor="#FFFFFF" HorizontalAlign="Left" />
            <EditRowStyle BackColor="" Width="100%" />
            <SelectedRowStyle BackColor="" Font-Bold="true" ForeColor="" />
            <PagerStyle BackColor="" ForeColor="" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#17C5F7" ForeColor="#FFFFFF" Font-Bold="true" HorizontalAlign="Left" />
            <AlternatingRowStyle BackColor="#B6EEFF" />
        </asp:GridView>
    </div>
    </form>

.CS Page code :

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace company
{
    public partial class QueryResult : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string UNAME = Convert.ToString(Session["USER_NAME"]);

            string printScript =
            @"function PrintGridView()
            {
            var gridInsideDiv = document.getElementById('VQGridView');
            var printWindow = window.open('VQGridView.htm','PrintWindow','letf=0,top=0,width=1000,height=700,toolbar=1,scrollbars=1,status=1');
            printWindow.document.write(gridInsideDiv.innerHTML);
            printWindow.document.close();
            printWindow.focus();
            printWindow.print();
            printWindow.close();}";
            this.ClientScript.RegisterStartupScript(Page.GetType(), "PrintGridView", printScript.ToString(), true);
            ImgBtnPrint.Attributes.Add("onclick", "PrintGridView();");

           
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["connecton string here"].ConnectionString;
            cn.Open();
            string QUERY = " Your Sql Query";
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(QUERY, cn);
            adp.Fill(dt);
            GridViewResult.DataSource = dt;
            GridViewResult.DataBind();
            cn.Close();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }

        protected void ImgBtnWord_Click(object sender, ImageClickEventArgs e)
        {
            GridViewResult.AllowPaging = false;
            GridViewResult.DataBind();
            Response.ClearContent();
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Visitors.doc"));
            Response.Charset = "";
            Response.ContentType = "application/ms-word";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridViewResult.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

        protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Visitors.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridViewResult.AllowPaging = false;
            GridViewResult.DataBind();
            //Change the Header Row back to white color
            GridViewResult.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < GridViewResult.HeaderRow.Cells.Count; i++)
            {
                GridViewResult.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
            }
            int j = 1;
            //This loop is used to apply stlye to cells based on particular row
            foreach (GridViewRow gvrow in GridViewResult.Rows)
            {
                //gvrow.BackColor = Color.White;
                if (j <= GridViewResult.Rows.Count)
                {
                    if (j % 2 != 0)
                    {
                        for (int k = 0; k < gvrow.Cells.Count; k++)
                        {
                            gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                        }
                    }
                }
                j++;
            }
            GridViewResult.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

        protected void ImgBtnBack_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("back.aspx");
        }
    }

}

18 Feb 2014

Append a MenuStrip to an MDI Parent Window (Windows Forms) c# (or) mdi child menu in wpf

In this post, I explained how to bind data to menu strip in MDI PARENT FORM. multiple-document interface (MDI) child window can be different from the MDI parent window. For example, the MDI parent might be a spreadsheet, and the MDI child might be a chart, I have used stored procedures in this article.  I have binded the menu strip based on the role of the user.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace Company
{
    public partial class MainForm : Telerik.WinControls.UI.RadForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public DataSet getMenu()
        {
            return SqlHelper.ExecuteDataset(Program.Con, CommandType.StoredProcedure, "Storedprocedute_Menu");
        }

        public DataSet getWorkMenu(string role, int menu)
        {
            SqlParameter[] cmdpar = new SqlParameter[2];
            cmdparam[0] = new SqlParameter("@p_menu", SqlDbType.Int);
            cmdparam[0].Value = menuid;
            cmdparam[1] = new SqlParameter("@p_role", SqlDbType.Int);
            cmdparam[1].Value = roleId;
            return SqlHelper.ExecuteDataset(Program.Con, CommandType.StoredProcedure, "StoredProcedure", cmdpar);
        }

        private void SelectedChildMenu_OnClick(object sender, System.EventArgs e)
        {
            if (((ActiveMdiChild != null))) ActiveMdiChild.Close();
            cmbNames.SelectedValue = sender.ToString();
            string formName = cmbNames.Text;
            string[] frms = formName.Split('&');

            object oForm = new object();
            Program.frmName = frms[0];
            Program.mode = frms[1];
            string projName = "Company";
            Type tForm = System.Reflection.Assembly.GetExecutingAssembly().GetType(projName + "." + frms[0]);

            if (((tForm != null)))
            {
                oForm = Activator.CreateInstance(tForm);
                ((Form)oForm).MdiParent = this;
                ((Form)oForm).Show();
            }
        }

        public void MainForm_Load(object sender, EventArgs e)
        {
            ToolStripMenuItem childItem = default(ToolStripMenuItem);
            int cnt = 0;
            DataSet menuData = null;
            menuData = getMenu();
            cmbFuncNames.DataSource = menuData.Tables[1];
            cmbFuncNames.DisplayMember = "progam";
            cmbFuncNames.ValueMember = "work";
            for (int j = 0; j <= menuData.Tables[0].Rows.Count - 1; j++)
            {
                int id = Convert.ToInt32(menuData.Tables[0].Rows[j]["MID"]);
                ToolStripMenuItem parItem = new ToolStripMenuItem();
                parItem.Name = id.ToString();
                string menuName = menuData.Tables[0].Rows[j]["MNAME"].ToString();
                parItem.Text = menuName;                 MenuStrip2.Items.Add(pItem);
                DataSet childDs = new DataSet();
                childDs = getWorkMenu(Program.userrole, id);
                ContextMenuStrip cms = new ContextMenuStrip();
                for (int i = 0; i <= childDs.Tables[0].Rows.Count - 1; i++)
                {
                    childItem = new ToolStripMenuItem();
                    childItem.ForeColor = System.Drawing.Color.Black;
                    childItem.Name = childDs.Tables[0].Rows[i]["WorkID"].ToString();
                    childItem.Text = childDs.Tables[0].Rows[i]["WorkNAME"].ToString();
                    childItem.Tag = childDs.Tables[0].Rows[i]["PROG_NAME"].ToString();
                    pItem.DropDownItems.Add(childItem);

                    cms.Items.Add(childItem.Text, null, new System.EventHandler(SelectedChildMenu_OnClick));
                }
                ToolStripMenuItem tsi = (ToolStripMenuItem)parItem;
                tsi.DropDown = cms;
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void MenuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "close")
            {
                Application.Exit();
            }
        }
    }
}