Thursday 27 October 2011

How To Edit,Update, Cancel, and Mail in Your GridView
















<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            onrowcancelingedit="GridView1_RowCancelingEdit"

            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"

            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"

            CellPadding="3" GridLines="Vertical"

            onselectedindexchanged="GridView1_SelectedIndexChanged"

            onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting">

            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />

            <Columns>

                <asp:TemplateField HeaderText="Emp_id">

                <ItemTemplate>

                <asp:Label runat="server" Text='<% #Eval("Emp_id") %>' ID = "id"></asp:Label>

                </ItemTemplate>

               

                </asp:TemplateField>

               

                <asp:TemplateField HeaderText="Emp_Name">

                    <ItemTemplate>

                        <asp:Label ID="Label1" runat="server" Text='<% #Eval("Emp_Name") %>'></asp:Label>

                    </ItemTemplate>

                    <EditItemTemplate>

                    <asp:TextBox ID="TextBox2" runat="server" Text='<% #Eval("Emp_Name") %>'></asp:TextBox>

                    </EditItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Emp_Salary">

                    <ItemTemplate>

                        <asp:Label ID="Label2" runat="server" Text='<% #Eval("Emp_Salary") %>'></asp:Label>

                    </ItemTemplate>

                    <EditItemTemplate>

                        <asp:TextBox ID="TextBox3" runat="server" Text='<% #Eval("Emp_Salary") %>'></asp:TextBox>

                    </EditItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Action">

                    <EditItemTemplate>

                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>

                        &nbsp;

                        <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>

                    </EditItemTemplate>

                    <ItemTemplate>

                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>

                        &nbsp;

                        <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Delete"

                            onclick="LinkButton4_Click">Delete</asp:LinkButton>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />

            <AlternatingRowStyle BackColor="#DCDCDC" />

        </asp:GridView>


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.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

namespace GridViewEditUpdateMail
{
    public partial class _Default : System.Web.UI.Page
    {
SqlConnection cn = new SqlConnection("server = JEET-PC\\JEET; user = sa; password = dhakad; database = jeet");
        SqlDataAdapter da; 
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_data();
            }
        }
        public void fill_data()
        {
            String sql = "select * from Emp";
            da = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            fill_data();
        }

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            fill_data();
        }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            String sql;

string id ((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text;
string tx = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string tx2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;

sql = "update Emp set emp_name =@a,emp_salary = @b where emp_id =@c";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Parameters.AddWithValue("@a", tx);
            cmd.Parameters.AddWithValue("@b", tx2);
            cmd.Parameters.AddWithValue("@c", id);
            cn.Open();
            cmd.ExecuteNonQuery();
            fill_data(); 

Send_Mail("This is Test Mail", "Hi jeetu Your Udated Name is ="+tx+" and your updated Salary is ="+tx2, "jeetu_choudhary13@gmail.com", "vishal_verma54@yahoo.com");
}


public void Send_Mail(String sub, String Body, String From, String To)
        {
            try
            {
                MailMessage mail = new MailMessage();
                //mail.To.Add("vishal_verma54@yahoo.com");
                mail.To.Add(To);
               // mail.From = new MailAddress("jeetu.choudhary13@gmail.com");
                mail.From = new MailAddress(From);
                //mail.Subject = "test mail";
                mail.Subject = sub;
                //mail.Body = "hi i am jeetu";
                mail.Body = Body;
                SmtpClient smtp = new SmtpClient();
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Host = "smtp.gmail.com";
                smtp.Credentials = new System.Net.NetworkCredential("jeetu.choudhary13@gmail.com", "*******");
                smtp.EnableSsl = true;
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                Response.Write("this is exception " + ex.Message);
            }

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}

No comments:

Post a Comment