About Me

My photo
New Delhi, Delhi, India
I am working in Infosys.

May 25, 2015

Grid Inhtml

<script type="text/javascript">
        function ValidateFiled() {
            var DecimalPatter = /^[0-9]*(\.[0-9]{1,20})?$/;
            var numericalPatter = /^[0-9]+$/;
            var mailPattern = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
            var MobilePattern = /^([0-9]{10})$/;
            var alphabatePattern = /^[a-zA-Z]+$/;
            var PasswordPattern = /^[\s\S]{8,}$/;
            var regPostcode = /^([1-9])([0-9]){5}$/;
            var websitepattern = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;

            var Name = document.getElementById("<%=txtName.ClientID %>").value;
            var Mobile = document.getElementById("<%=txtMobile.ClientID %>").value;
            var EmailID = document.getElementById("<%=txtEmailID.ClientID %>").value;
            var City = document.getElementById("<%=ddlCity.ClientID %>").value;
            if (Name == "" || Name == null) {
                document.getElementById("lbl_Msg").style.display = '';
                document.getElementById("lbl_Msg").innerHTML = "Please Enter  Name !";
                return false;
            }
            else if (Mobile.match(MobilePattern) == "" || Mobile.match(MobilePattern) == null) {
                document.getElementById("lbl_Msg").style.display = '';
                document.getElementById("lbl_Msg").innerHTML = "Please Enter  Mobileno !";
                return false;
            }
            else if (EmailID.match(mailPattern) == "" || EmailID.match(mailPattern) == null) {
                document.getElementById("lbl_Msg").style.display = '';
                document.getElementById("lbl_Msg").innerHTML = "Please Enter  EmailID !";
                return false;
            }
            else if (City == "" || City == null) {
                document.getElementById("lbl_Msg").style.display = '';
                document.getElementById("lbl_Msg").innerHTML = "Please select city !";
                return false;
            }

        }
    </script>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>
        New Employee Registration
    </h1>
    </div>
        <div>
            <table style="margin-left:40%">
                <tr style="visibility:hidden">
                    <td >EmpID</td>
                    <td>
                        <asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Name:</td>
                    <td>
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>DOB</td>
                    <td>
                        <asp:TextBox ID="txtDOB" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>EmailID</td>
                    <td>
                        <asp:TextBox ID="txtEmailID" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Mobile</td>
                    <td>
                        <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Department</td>
                    <td>
                        <asp:DropDownList ID="ddlDepartment" runat="server" Height="16px" Width="127px">
                         
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td>
                        <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lbl_Msg" ForeColor="Red" runat="server" Text=""></asp:Label>

                    </td>
                    <td>
                        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                        &nbsp;
                        <asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click"  />

                    </td>
                </tr>
              
            </table>
        </div>
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated" >
                <Columns>
                    <asp:TemplateField HeaderText="EmpID">
                        <ItemTemplate>
                            <asp:Label ID="lbl_EmpID" runat="server" Text='<%#Eval("EmpID") %>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="DOB">
                        <ItemTemplate>
                            <asp:Label ID="lbl_DOB" runat="server" Text='<%#Eval("DOB") %>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="EmailID">
                        <ItemTemplate>
                            <asp:Label ID="lblEmailID" runat="server" Text='<%#Eval("EmailID") %>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Mobile">
                        <ItemTemplate>
                            <asp:Label ID="lbl_Mobile" runat="server" Text='<%#Eval("Mobile") %>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="DepetName">
                        <ItemTemplate>
                            <asp:Label ID="lbl_DepetName" runat="server" Text='<%#Eval("DepetName") %>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Address">
                        <ItemTemplate>
                            <asp:Label ID="lbl_Address" runat="server" Text='<%#Eval("Address") %>'>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" CommandName="Edit"  CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" Text="Edit" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("EmpID") %>' OnClientClick="return confirm('Are You sure want delete')" CausesValidation="false" Text="Delete" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#330099" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                <SortedAscendingCellStyle BackColor="#FEFCEB" />
                <SortedAscendingHeaderStyle BackColor="#AF0101" />
                <SortedDescendingCellStyle BackColor="#F6F0C0" />
                <SortedDescendingHeaderStyle BackColor="#7E0000" />

            </asp:GridView>
        </div>
    </form>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;


public partial class Employee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if  (!Page.IsPostBack)
        {
            FillDepartment();
            FillGrid();
        }
    }
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ToString());
    SqlCommand cmd = new SqlCommand();
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("Proc_Employee", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@AA", 1);
            cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text);
            cmd.Parameters.AddWithValue("@Name", txtName.Text.ToString());
            cmd.Parameters.AddWithValue("@DOB", txtDOB.Text.ToString());
            cmd.Parameters.AddWithValue("@EmailID", txtEmailID.Text.ToString());
            cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text.ToString());
            cmd.Parameters.AddWithValue("@DepetName", ddlDepartment.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@Address", txtAddress.Text.ToString());
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ClearField();
            FillGrid();
            lbl_Msg.Text = "Save successfully";
        }
        catch (Exception ex)
        {
            lbl_Msg.Text = ex.Message.ToString();
        }

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        ClearField();
    }
    private void FillDepartment()
    {
        cmd = new SqlCommand("Proc_Employee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@AA", 2);
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        con.Close();
        ddlDepartment.DataSource = ds;
        ddlDepartment.DataTextField = "DeptName";
        ddlDepartment.DataBind();


    }
    private void FillGrid()
    {
        cmd = new SqlCommand("Proc_Employee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@AA", 3);
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();




    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edit")
            {
                int RowIndex = int.Parse(e.CommandArgument.ToString());
                Label lbl_EmpID = (Label)GridView1.Rows[RowIndex].FindControl("lbl_EmpID");
                Label lbl_Name = (Label)GridView1.Rows[RowIndex].FindControl("lbl_Name");
                Label lbl_DOB = (Label)GridView1.Rows[RowIndex].FindControl("lbl_DOB");
                Label lblEmailID = (Label)GridView1.Rows[RowIndex].FindControl("lblEmailID");
                Label lbl_Mobile = (Label)GridView1.Rows[RowIndex].FindControl("lbl_Mobile");
                Label lbl_DepetName = (Label)GridView1.Rows[RowIndex].FindControl("lbl_DepetName");
                Label lbl_Address = (Label)GridView1.Rows[RowIndex].FindControl("lbl_Address");
                txtEmpID.Text = lbl_EmpID.Text;
                txtName.Text = lbl_Name.Text;
                txtDOB.Text = lbl_DOB.Text;
                txtEmailID.Text = lblEmailID.Text;
                txtMobile.Text = lbl_Mobile.Text;
                ddlDepartment.Text = lbl_DepetName.Text;
                txtAddress.Text = lbl_Address.Text;
            
              

            }
            if (e.CommandName == "Delete")
            {
                cmd = new SqlCommand("Proc_Employee", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@AA", 4);
                cmd.Parameters.AddWithValue("@EmpID", Convert.ToInt32(e.CommandArgument.ToString()));
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                lbl_Msg.Text = "Delete Sucessfully";


            }
        }
        catch (Exception ex)
        {

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView1.EditIndex = -1;
        FillGrid();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        FillGrid();
    }
    private void ClearField()
    {
        txtAddress.Text = "";
        txtDOB.Text = "";
        txtEmailID.Text = "";
        txtEmpID.Text = "";
        txtMobile.Text = "";
        txtName.Text = "";
        lbl_Msg.Text = "";
        //ddlDepartment.SelectedValue = "";
    }


    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        FillGrid();
    }
}