Imp Code.


Q. GridView Edit, Update, Delete Code.?
<form id="form1" runat="server">
    <div>
    <table style="margin-left: 480px">
    <tr>
    <td>
    <asp:Label ID="lblName" runat="server" Text="Name :" Font-Bold="true"></asp:Label>
    </td><td></td>
    <td><asp:TextBox ID="txtName" runat="server" Text="" Font-Bold="true"></asp:TextBox></td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="lblEmail" runat="server" Text="Email Id :" Font-Bold="true"></asp:Label>
    </td><td></td>
    <td><asp:TextBox ID="txtEmailId" runat="server" Text="" Font-Bold="true"></asp:TextBox></td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="lblAddress" runat="server" Text="Address :" Font-Bold="true"></asp:Label>
    </td><td></td>
    <td><asp:TextBox ID="txtAddress" runat="server" Text="" Font-Bold="true"></asp:TextBox></td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="lblProfesssion" runat="server" Text="Profession :" Font-Bold="true"></asp:Label>
    </td><td></td>
    <td><asp:TextBox ID="txtProfession" runat="server" Text="" Font-Bold="true"></asp:TextBox></td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="lblMobNo" runat="server" Text="Mobile No :" Font-Bold="true"></asp:Label>
    </td><td></td>
    <td><asp:TextBox ID="txtMobNo" runat="server" Text="" Font-Bold="true"></asp:TextBox></td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Bold="true" 
            onclick="btnSubmit_Click" />
    <asp:Button ID="btnCancel" runat="server" Text="Cancel" Font-Bold="true" />
    </td>
    </tr>
    
    </table>
    <table style="margin-left: 480px">
    <div>
    <tr>
    <td align="center" style="margin-left: 280px">
    <asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false"  DataKeyNames="ID"
            onrowediting="gridView_RowEditing" onrowupdating="gridView_RowUpdating" 
            onrowcancelingedit="gridView_RowCancelingEdit" 
            onrowdeleting="gridView_RowDeleting">
    <Columns>
    <asp:TemplateField HeaderText="NAME">
    <ItemTemplate>
    <asp:Label id="lbName" runat="server" Text='<%#Eval("NAME") %>'>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtEditName" runat="server" Text='<%#Eval("NAME") %>'>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>    
    <asp:TemplateField HeaderText="EMAIL ID">
    <ItemTemplate>
    <asp:Label id="lbEmailId" runat="server" Text='<%#Eval("EmailID") %>'>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtEditEmailId" runat="server" Text='<%#Eval("EmailID") %>'>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ADDRESS">
    <ItemTemplate>
    <asp:Label id="lbAddress" runat="server" Text='<%#Eval("Address") %>'>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtEditAddress" runat="server" Text='<%#Eval("Address") %>'>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="PROFESSION">
    <ItemTemplate>
    <asp:Label id="lbProfession" runat="server" Text='<%#Eval("Profession") %>'>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtEditProfession" runat="server" Text='<%#Eval("Profession") %>'>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="MOBILE NO">
    <ItemTemplate>
    <asp:Label id="lbMobNo" runat="server" Text='<%#Eval("MobileNo") %>'>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtEditMobNo" runat="server" Text='<%#Eval("MobileNo") %>'>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Edit">
    <ItemTemplate>
    <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:LinkButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update"></asp:LinkButton>
    <asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel"></asp:LinkButton>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
    <asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </td>
    </tr>    
    </div>
    </table>    
    </div>        
    </form>

=================================

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

namespace GridViewExp
{
    public partial class Grid : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Contstring"].ToString());

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        protected void BindGrid()
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM EmpLogin", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gridView.DataSource = dt;
            gridView.DataBind();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string insertQuery = "INSERT INTO EmpLogin(NAME,EmailID,Address,Profession,MobileNo)VALUES('" + txtName.Text + "','" + txtEmailId.Text + "','" + txtAddress.Text + "','" + txtProfession.Text + "','" + txtMobNo.Text + "')";
                SqlCommand cmd = new SqlCommand(insertQuery, con);
                con.Open();
                cmd.ExecuteNonQuery();
                BindGrid();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }

        protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gridView.EditIndex = e.NewEditIndex;
            BindGrid();
        }
        protected void gridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridView.EditIndex = -1;
            BindGrid();
        }
        protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int id= Convert.ToInt32(gridView.DataKeys[e.RowIndex].Value.ToString());
                TextBox Name = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtEditName");
                TextBox EmailID = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtEditEmailId");
                TextBox Address = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtEditAddress");
                TextBox Profession = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtEditProfession");
                TextBox MobileNo = (TextBox)gridView.Rows[e.RowIndex].FindControl("txtEditMobNo");
                string UpdateQuery = "UPDATE  EmpLogin SET NAME='" + Name.Text + "',EmailID='" + EmailID.Text + "',Address='" + Address.Text + "',Profession='" + Profession.Text + "',MobileNo='" + MobileNo.Text + "' WHERE ID="+id;
                SqlCommand cmd = new SqlCommand(UpdateQuery,con);
                con.Open();
                cmd.ExecuteNonQuery();
                gridView.EditIndex = -1;
                con.Close();
                Response.Write("Data Updated Sucessfully..!");
                BindGrid();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(gridView.DataKeys[e.RowIndex].Values["ID"].ToString());
                string _deleteQuery = "DELETE FROM EmpLogin WHERE ID=" + id;
                SqlCommand cmd = new SqlCommand(_deleteQuery, con);
                con.Open();
                cmd.ExecuteNonQuery();
                BindGrid();
                Response.Write("Records has been deleted sucessfully..!");
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }         
        }      
    }
}
==================================
Web Config Setting....
<appSettings>
    <add key="Contstring" value="Data Source=MANU-PC\MYDB;Initial Catalog=TEST;Integrated Security=True"/>

  </appSettings>

==================================
Regex for multiple email validation. 
Regex(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");

==================================

1 comment:

  1. I Like it Dost...... Yeh kuch alag hai bhai, kishi ki kaam ayegi :)

    ReplyDelete

Powered by Blogger.