Wednesday 28 November 2012

Export data in PDF formate form GridView

        Export data in PDF formate form GridView

     void ExportPDF1()
     {
        GridView grdview1 = new GridView();
        CssClass_GridView(grdview1);
        SqlConnection conn = new SqlConnection(con);
        SqlCommand cmd = new SqlCommand("fetchDataForReportnew", conn);
        cmd.Parameters.AddWithValue("@empid", Convert.ToInt64(Session["EmpId"]));
        cmd.Parameters.AddWithValue("@Curr_Emp", Session["UserName"].ToString());
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        Document pdfDoc = new Document(PageSize.A4, 9f, 9f, 9f, 0f);
        System.IO.MemoryStream msReport = new System.IO.MemoryStream();

        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, msReport);

        if (dt.Rows.Count > 0)
        {
            grdview1.DataSource = dt;
            grdview1.DataBind();
        }
        StringWriter sw0 = new StringWriter();
        HtmlTextWriter hw0 = new HtmlTextWriter(sw0);
        if (grdview1 != null && grdview1.Rows.Count > 0)
        {

            string filename = Session["UserName"].ToString() + "_Expense_" + DateTime.Now.Ticks + ".pdf";
            string file_Path = "~\\ExportFiles\\" + filename;
            PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath(file_Path), FileMode.Create));
            pdfDoc.Open();

            PdfPTable table = new PdfPTable(6);
            table.TotalWidth = 578f;
            table.LockedWidth = true;
            PdfPCell Header1 = new PdfPCell(new Phrase("Travel Record", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL)));
            Header1.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell Header2 = new PdfPCell(new Phrase("DATE OF REQUISITION", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL)));
            Header1.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell name = new PdfPCell(new Phrase(dt.Rows[0]["Requisition Date"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL)));
            name.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell territory = new PdfPCell(new Phrase("NAME", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL)));
            territory.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell terriname = new PdfPCell(new Phrase(dt.Rows[0]["Employee"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL)));
            terriname.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell submitdate = new PdfPCell(new Phrase("TERRITORY", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL)));
            territory.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell date_submit = new PdfPCell(new Phrase(dt.Rows[0]["Branch"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL)));
            terriname.HorizontalAlignment = Element.ALIGN_CENTER;

            PdfPCell Date = new PdfPCell(new Phrase("From", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
            Date.BackgroundColor = new Color(80, 124, 209);
            Date.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell AreaVisited = new PdfPCell(new Phrase("To", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
            AreaVisited.BackgroundColor = new Color(80, 124, 209);
            AreaVisited.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell CustomerName = new PdfPCell(new Phrase("Departure Date", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
            CustomerName.BackgroundColor = new Color(80, 124, 209);
            CustomerName.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell Product = new PdfPCell(new Phrase("Mode Of Travel", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
            Product.BackgroundColor = new Color(80, 124, 209);
            Product.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell Purpose = new PdfPCell(new Phrase("Purpose of visit", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
            Purpose.BackgroundColor = new Color(80, 124, 209);
            Purpose.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell mode = new PdfPCell(new Phrase("Fare",FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, Color.WHITE)));
            mode.BackgroundColor = new Color(80, 124, 209);
            mode.HorizontalAlignment = Element.ALIGN_CENTER;

            Header1.Colspan = 6;
            table.AddCell(Header1);
            Header2.Colspan = 2;
            table.AddCell(Header2);
            name.Colspan = 4;
            name.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(name);
            territory.Colspan = 2;
            territory.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(territory);
            terriname.Colspan = 4;
            terriname.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(terriname);
            submitdate.Colspan = 2;
            submitdate.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(submitdate);
            date_submit.Colspan = 4;
            date_submit.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(date_submit);
            table.AddCell(Date);
            table.AddCell(AreaVisited);
            table.AddCell(CustomerName);
            table.AddCell(Product);
            table.AddCell(Purpose);
            table.AddCell(mode);
           

            pdfDoc.Add(table);
            iTextSharp.text.Table datatable = new iTextSharp.text.Table(6);
            datatable.Width = 100f;
            datatable.Padding = 1;
            datatable.Border = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                Chunk dateofvisit = new Chunk(dt.Rows[i]["From Location"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
                Cell date = new Cell(dateofvisit);
               
                date.HorizontalAlignment = Element.ALIGN_CENTER;
                datatable.AddCell(date);
                Chunk area_visited = new Chunk(dt.Rows[i]["To Location"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
                Cell areavisited = new Cell(area_visited);
                areavisited.HorizontalAlignment = Element.ALIGN_CENTER;
                datatable.AddCell(areavisited);
                Chunk customer_name = new Chunk(dt.Rows[i]["DateOfVisit"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
                Cell customername = new Cell(customer_name);
                customername.HorizontalAlignment = Element.ALIGN_CENTER;
                datatable.AddCell(customername);
                Chunk product = new Chunk(dt.Rows[i]["ModeOfTravel"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
                Cell product1 = new Cell(product);
                product1.HorizontalAlignment = Element.ALIGN_CENTER;
                datatable.AddCell(product1);
                Chunk purpose = new Chunk(dt.Rows[i]["Purpose Of Visit"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
                Cell purpose1 = new Cell(purpose);
                purpose1.HorizontalAlignment = Element.ALIGN_CENTER;
                datatable.AddCell(purpose1);
                Chunk chunk = new Chunk(dt.Rows[i]["Fare"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
                Cell cell = new Cell(chunk);
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                datatable.AddCell(cell);
            }

            decimal grdtotal = 0;
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                string gd = dt.Rows[r]["Fare"].ToString();
                if (gd != "")
                    grdtotal += Convert.ToDecimal(gd);
            }
         
            Chunk Gtotal2 = new Chunk("Grand Total (Rs.)", FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.BOLD));
            Cell Grand_total12 = new Cell(Gtotal2);
            Grand_total12.Colspan = 5;
            Grand_total12.HorizontalAlignment = Element.ALIGN_RIGHT;
            datatable.AddCell(Grand_total12);

            Chunk Gtotal1 = new Chunk(grdtotal.ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 7, Font.NORMAL));
            Cell Grand_total = new Cell(Gtotal1);
            Grand_total.HorizontalAlignment = Element.ALIGN_CENTER;
            datatable.AddCell(Grand_total);

            pdfDoc.Add(datatable);
            Session["file_name"] = filename;
            Session["file_path"] = file_Path;
            Session["type"] = "Travel";
            pdfDoc.Close();
}

Export data from Gridview to Excel in Asp.net

Export data from Gridview to Excel in Asp.net

protected void btnexport_Click(object sender, EventArgs e)
    {
        //first method
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=GridView_to_Excel.xls");   
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
        grdview.Columns.RemoveAt(6);
        grdview.RenderControl(HtmlTextWriter);
        Response.Write(StringWriter.ToString());
        Response.End();

      //second method

    ////    dbconnect ob = new dbconnect();
    ////    int colIndex = 1;
    ////    int rowIndex = 1;
    ////    Excel.Application xlApp;
    ////    Excel.Workbook xlWorkBook;
    ////    Excel.Worksheet xlWorkSheet;
    ////    object misValue = System.Reflection.Missing.Value;
    ////    xlApp = new Excel.Application();
    ////    Excel.Range ExelRange;
   
    ////    DataTable dt = new DataTable();
    ////    dt = ob.Gridbind();

    ////    xlApp = new Excel.Application();
    ////    xlWorkBook = xlApp.Workbooks.Add(misValue);
    ////    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    ////    foreach (DataRow theRow in dt.Rows)
    ////    {
    ////        rowIndex = rowIndex + 1;
    ////        colIndex = 0;
    ////        foreach (DataColumn dc in dt.Columns)
    ////        {
    ////            colIndex = colIndex + 1;
    ////            xlWorkSheet.Cells[rowIndex + 1, colIndex] = theRow[dc.ColumnName];
    ////            xlWorkSheet.Rows.AutoFit();
    ////            xlWorkSheet.Columns.AutoFit();
    ////        }
    ////    }

    ////    xlWorkSheet.get_Range("b2", "e2").Merge(false);

    ////    ExelRange = xlWorkSheet.get_Range("b2", "e2");
    ////    ExelRange.FormulaR1C1 = "Exel Title or Table Name ";

    ////    ExelRange.HorizontalAlignment = 3;
    ////    ExelRange.VerticalAlignment = 3;

    ////    xlApp.Visible = true;
    ////    ObjectRelease(xlWorkSheet);
    ////    ObjectRelease(xlWorkBook);
    ////    ObjectRelease(xlApp);



    ////}

    ////private void ObjectRelease(object objRealease)
    ////{
    ////try
    ////{
    ////    System.Runtime.InteropServices.Marshal.ReleaseComObject(objRealease);
    ////    objRealease = null;
    ////}
    ////catch (Exception ex)
    ////{
    ////    objRealease = null;
      
    ////}
    ////finally
    ////{
    ////    GC.Collect();
    ////}
    }


    public override void VerifyRenderingInServerForm(Control control)
    {

        /* Verifies that the control is rendered */

    }
  

Tuesday 6 November 2012

Sending Email in Asp.net with Attachment and without Attachment


1)without attachment
.aspx code

<html>
<head></head>
<body>
<div>
<br />
<table>
<tr>
<td>To</td><td><asp:TextBox ID="txtto" runat="server" Width="200px"></asp:TextBox></td>
</tr>
<tr>
<td>Subject</td><td><asp:TextBox ID="txtsubject" runat="server" Width="250px"></asp:TextBox></td>
</tr>
<tr>
<td>Message</td><td><asp:TextBox ID="txtmessage" runat="server" Width="350px" TextMode="MultiLine" Height="60px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="lblmsg" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td colspan="2">
<center>
<asp:Button ID="bensave" runat="server" Text="Send" onclick="bensave_Click" />
</center>
</td>
</tr>
</table>
</div>
</body>
</html>

.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

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

    }
    protected void bensave_Click(object sender, EventArgs e)
    {
        var mail = new MailMessage();
        mail.From = new MailAddress("salikrammaurya@gmail.com");
        mail.To.Add(txtto.Text);
        mail.Subject = txtsubject.Text;
        mail.IsBodyHtml = true;
        mail.Body =txtmessage.Text;
        mail.Priority = MailPriority.High;
        var mailClient = new SmtpClient("smtp.gmail.com", 587);
        mailClient.EnableSsl = true;
        mailClient.Credentials = new System.Net.NetworkCredential("salikrammaurya@gmail.com", "password");
        mailClient.Send(mail);
        lblmsg.Text = "Mail sent successfully!";
        txtto.Text = txtsubject.Text = txtmessage.Text = string.Empty;
    }
}

2) With Attachment

.aspx code

<html>
<head></head>
<body>
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail with multiple Attachments using asp.net</b>
</td>
</tr>
<tr>
<td>To</td><td><asp:TextBox  ID="txtto" runat="server" Width="200px" ></asp:TextBox></td>
</tr>
<tr>
<td>Subject</td><td><asp:TextBox  ID="txtsubject" runat="server" Width="200px" ></asp:TextBox></td>
</tr>
<tr>
<td>Message</td><td><asp:TextBox  ID="txtmessage" runat="server" Width="200px" TextMode="MultiLine" ></asp:TextBox></td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" /><br />
<asp:FileUpload ID="fileUpload2" runat="server" /><br />
<asp:FileUpload ID="fileUpload3" runat="server" /><br />
<asp:FileUpload ID="fileUpload4" runat="server" /><br />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</body>
</html>

.cs code

using System;
using System.Data;
using System.Configuration;
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.Linq;
using System.Net.Mail;

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

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

        MailMessage mail = new MailMessage();
        mail.To.Add(txtto.Text);
        mail.From = new MailAddress("salikrammaurya@gmail.com");
        mail.Subject = txtsubject.Text;
        mail.Body = txtmessage.Text;
        mail.IsBodyHtml = true;

        //Attach file using FileUpload Control and put the file in memory stream
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }
        if (fileUpload2.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload2.PostedFile.InputStream, fileUpload2.FileName));
        }
        if (fileUpload3.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload3.PostedFile.InputStream, fileUpload3.FileName));
        }
        if (fileUpload4.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload4.PostedFile.InputStream, fileUpload4.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential("salikrammaurya@gmail.com", "password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
  
    }

}

Thursday 1 November 2012

Jquery For Calender

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.8.22/themes/base/jquery-ui.css" type="text/css" media="all" />
            <link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
            <script src="http://code.jquery.com/ui/1.8.22/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function ($) {


        $(function () {
            $.datepicker.setDefaults($.datepicker.regional[""]);
            $(".datepicker").datepicker({ yearRange: "1901:2050"
, changeMonth: true,
                changeYear: true
            });


        });
        // Handler for .ready() called.
    });
  </script>
<style type="text/css">
    .datepicker
    {
        height:20px;
        width:150px;
    }
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
    <table>
    <tr>
    <td>DOB:</td>
    <td><asp:TextBox ID="txtdob" runat="server" class="datepicker"></asp:TextBox></td>
    </tr>
    <tr>
    <td>DOJ:</td>
    <td><asp:TextBox ID="txtdoj" runat="server" class="datepicker"></asp:TextBox></td>
    </tr>
    <tr>
    <td colspan="2">
    <center>
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
    </center>
    </td>
    </tr>
    </table>
    </center>
    </div>
    </form>
</body>
</html>

Dynamically Add and Remove Rows In GridView In Asp.Net


.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicGridview.aspx.cs" Inherits="DynamicGridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            ShowFooter="true" onrowcommand="GridView1_RowCommand">
        <AlternatingRowStyle BackColor="#CCCCFF" BorderColor="#003366"
            BorderStyle="Solid" BorderWidth="1px" />
    <Columns>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Phone">
    <ItemTemplate>
    <asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
    </ItemTemplate>
    <FooterTemplate>
    <center>
    <asp:Button ID="btnadd" runat="server" Text="Add New" CommandName="addnew" />
    </center>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Email">
    <ItemTemplate>
    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Address">
    <ItemTemplate>
    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
    <asp:Button ID="btndelete" runat="server" Text="Delete" CommandName="deleted" />
    </ItemTemplate>
    </asp:TemplateField>             
    </Columns>
        <EmptyDataRowStyle BackColor="White" />
        <FooterStyle BackColor="#336699" />
        <HeaderStyle BackColor="#336699" ForeColor="White" />
    </asp:GridView>
    </center>
    </div>
    </form>
</body>
</html>

.cs Code
--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class DynamicGridview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind_grid();
        }

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName == "addnew")
        {
            addnewRow();
         
        }

        if (e.CommandName == "deleted")
        {
            Button btn = (Button)e.CommandSource;
            if (btn != null)
            {
                GridViewRow grdrow = (GridViewRow) btn.NamingContainer;
                int row = grdrow.RowIndex;
                deleteRow();
                DataTable dtt = (DataTable)ViewState["deleteRow"];

                if (dtt.Rows.Count > 1)
                {
                    dtt.Rows.RemoveAt(row);
                    dtt.AcceptChanges();
                }

                GridView1.DataSource = dtt;
                GridView1.DataBind();
               
            }

        }
    }
    public void bind_grid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn ("Phone",typeof(string)));
        dt.Columns.Add(new DataColumn("Email", typeof(string)));
        dt.Columns.Add(new DataColumn("Address",typeof(string)));

        DataRow dr = dt.NewRow();
      
        dt.Rows.Add(dr);
        ViewState["CurrentTable"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    public void addnewRow()
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.Add(new DataColumn("Name", typeof(string)));
        dt1.Columns.Add(new DataColumn("Phone", typeof(string)));
        dt1.Columns.Add(new DataColumn("Email", typeof(string)));
        dt1.Columns.Add(new DataColumn("Address", typeof(string)));

        for (int i = 0; i < GridView1.Rows.Count;i++ )
        {
            TextBox txt_name = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtname");
            TextBox txt_phone = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtphone");
            TextBox txt_email = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtemail");
            TextBox txt_address = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtaddress");

            DataRow dr = dt1.NewRow();
            dr["Name"] = txt_name.Text;
            dr["Phone"] = txt_phone.Text;
            dr["Email"] = txt_email.Text;
            dr["Address"] = txt_address.Text;
            dt1.Rows.Add(dr);
            dt1.AcceptChanges();
        }
        DataRow dr1 = dt1.NewRow();
        dr1["Name"] = string.Empty;
        dr1["Phone"] = string.Empty;
        dr1["Email"] = string.Empty;
        dr1["Address"] = string.Empty;

        dt1.Rows.Add(dr1);
        ViewState["deleteRow"] = dt1;
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }

    public void deleteRow()
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.Add(new DataColumn("Name", typeof(string)));
        dt1.Columns.Add(new DataColumn("Phone", typeof(string)));
        dt1.Columns.Add(new DataColumn("Email", typeof(string)));
        dt1.Columns.Add(new DataColumn("Address", typeof(string)));

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            TextBox txt_name = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtname");
            TextBox txt_phone = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtphone");
            TextBox txt_email = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtemail");
            TextBox txt_address = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtaddress");

            DataRow dr = dt1.NewRow();
            dr["Name"] = txt_name.Text;
            dr["Phone"] = txt_phone.Text;
            dr["Email"] = txt_email.Text;
            dr["Address"] = txt_address.Text;
            dt1.Rows.Add(dr);
            dt1.AcceptChanges();
        }
      
        ViewState["deleteRow"] = dt1;
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}