Saturday, 14 April 2012

All In Three Tier (insert)(update,delete in gridview )with export to excel


//.aspx page-source page//

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"  EnableEventValidation ="false" Inherits="_Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
   
<%--<%@ Register Src="UI/footer.ascx" TagName="Footer" TagPrefix="uc3" %>--%>
<%@ Register Src="~/UI/Menu.ascx" TagName="menu" TagPrefix="uc4" %>

<!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>Untitled Page</title>
        <link href="Stylecal.css" rel="stylesheet" type="text/css" />
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    <script src="JScript.js" language="javascript" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
   
    <div>
       <asp:ScriptManager ID="ScriptManager1" runat="server">
                    </asp:ScriptManager>
      <asp:UpdatePanel ID="Updatepanel1" runat="server">
                <ContentTemplate>
        <table align="center" width="100%" class ="table-class" >
        <uc4:menu ID="u4" runat="server" />
       <tr >
       <td class="td-class" align="center">
           COMPANY<asp:DropDownList id="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
                    </asp:DropDownList>
       </td>
     
     
     
       </tr>
            <tr>
                <td   align="center">
                    PRODUCT<asp:DropDownList id="DropDownList2" runat="server">
                    </asp:DropDownList></td>
               
                <td class="td-class">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2" style="height: 22px">
                    <asp:Button ID="Button1" runat="server" Text="ok" OnClick="Button1_Click" CssClass="stamp" /></td>
            </tr>
            <tr>
                <td align="center" colspan="2">
               
                    <asp:SiteMapPath ID="SiteMapPath1" runat="server">
                   
                   
                 
                    </asp:SiteMapPath>
                 
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2" rowspan="1">
                </td>
            </tr>
            <tr>
                <td colspan="2" rowspan="4" align="center">
                    &nbsp;<asp:UpdatePanel ID ="up1" runat ="server">
               
               
            <ContentTemplate>
         
                    <asp:GridView ID="GridView1" runat="server" DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False" CssClass="GridHeading-text" OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
                        <Columns>
                            <asp:CommandField ShowDeleteButton="True" />
                            <asp:CommandField ShowEditButton="True" />
                            <asp:BoundField DataField="id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression = "id" Visible ="False"  />
                            <asp:BoundField DataField="companyname" HeaderText="CompanyName" />
                            <asp:BoundField DataField="productname" HeaderText="Product" />
                         
                        </Columns>
                    </asp:GridView>
                 
                    </ContentTemplate>
                    </asp:UpdatePanel> &nbsp;
                 
                </td>
            </tr>
           
            <tr>
            </tr>
            <tr>
            </tr>
           
            <tr>
                <td align="center" >
                    </td>
            </tr>
            <tr>
                <td align="center" colspan="2" rowspan="1">
                    <asp:Button ID="btn_export" runat="server" CssClass="stamp" ForeColor="White" OnClick="btn_export_Click"
                        Text="Export To Excel" /></td>
                <td align="center">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    &nbsp;
                 <br />
                 
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2" style="height: 21px">
                    &nbsp;</td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:Wizard ID="Wizard1" runat="server" ActiveStepIndex="0" FinishCompleteButtonImageUrl="1"
                        OnFinishButtonClick="Wizard1_FinishButtonClick1">
                        <WizardSteps>
                            <asp:WizardStep runat="server" Title="Step 1">
                                <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/XMLFile.xml"
                        Height="67px" Width="100px" />
                            </asp:WizardStep>
                            <asp:WizardStep runat="server" Title="Step 2">
                    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AllowPaging="True" OnPageIndexChanging="DetailsView1_PageIndexChanging" CssClass="GridHeading-text">
                 
                    </asp:DetailsView>
                            </asp:WizardStep>
                        </WizardSteps>
                    </asp:Wizard>
                    &nbsp;
                    &nbsp; &nbsp;</td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                </td>
            </tr>
            <tr>
                <td   align="center" colspan="2">
                    <asp:Button ID="Button2" runat="server" Text="submit" OnClick="Button2_Click" OnClientClick ="return mainprogram();" CssClass="stamp" />
                   
                    </td>
            </tr>
            <tr>
                <td  align="center" colspan="2" >
                    <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label></td>
            </tr>
            <tr >
            <td width="100%" class="td-class"><%--<uc3:footer id="Footer1" runat="server"></uc3:footer></td>--%>
            </tr>
        </table>
    </ContentTemplate>
   <Triggers>
                   <asp:PostBackTrigger ControlID="btn_export" />
                   
                </Triggers>
   </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
//.aspx.cs//

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using grid;
using System.Collections.Generic;

using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
            //cn.Open();
            DataSet ds = new DataSet();
            company cs = new company();
            bindgrid1();

            ds=company.show();
            //ds=cs.show();
           // SqlDataAdapter da = new SqlDataAdapter("select_sp", cn);
            //da.Fill(ds);
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "id";
            DropDownList1.DataBind();
            DropDownList1.Items .Insert(0, "--SELECT--");
            bindgrid();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

       // SqlConnection cn = new SqlConnection(ConfigurationManager .ConnectionStrings[1].ConnectionString );
        //cn.Open();
        //SqlCommand cmd = new SqlCommand("insertgridlist_sp", cn);
        //SqlCommand cmd = new SqlCommand("insert into gridlist values ('" + DropDownList1.SelectedItem.ToString() + "','" + DropDownList2.SelectedItem.ToString () + "')", cn);

        //cmd.Parameters.AddWithValue("@companyname", DropDownList1.SelectedItem.ToString());
        //cmd.Parameters.AddWithValue("@productname", DropDownList2.SelectedItem.ToString());
        //cmd.CommandType = CommandType.StoredProcedure;
        //cmd.ExecuteNonQuery();
        //cn.Close();
        company cs = new company();
        cs.companyname = DropDownList1.SelectedItem.ToString();
        cs.productname = DropDownList2.SelectedItem.ToString();
        cs.add();
        //DataSet ds = new DataSet();
        //SqlDataAdapter da = new SqlDataAdapter("select_grid_sp ", cn);
        //da.Fill(ds);
       
        //GridView1 .DataSource = ds.Tables[0];
        
        //GridView1 .DataBind();
        bindgrid();
        
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int aa=Convert.ToInt32( DropDownList1.SelectedValue.ToString());
        //SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
        //cn.Open();
        DataSet ds = new DataSet();
        //company cs = new company();
        //SqlDataAdapter da = new SqlDataAdapter("SELECT dbo.product.pname,dbo.product.id,dbo.company.id,dbo.product.pid FROM dbo.company INNER JOIN dbo.product ON dbo.company.id = dbo.product.id where product.id=" + aa, cn);
        //SqlCommand cmd = new SqlCommand("select_join_sp", cn);
        //cmd.Parameters.AddWithValue("@id", aa);

        //cmd.CommandType = CommandType.StoredProcedure;
       // SqlDataAdapter da = new SqlDataAdapter(cmd);
        //da.Fill(ds);
        ds = company.show1(aa);
        DropDownList2.DataSource = ds.Tables[0];
        DropDownList2.DataTextField = "pname";
        DropDownList2.DataValueField = "pid";
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, "--SELECT--");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }
    public void bindgrid()
    {
        //SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
        //cn.Open();
        DataSet ds = new DataSet();
       
        //SqlDataAdapter da = new SqlDataAdapter("select_grid_sp",cn);
        //da.Fill(ds);
        ds = company.Grid1();
        GridView1.DataSource = ds.Tables[0];

        GridView1.DataBind();
     

    }
    public void bindgrid1()
    {
        //SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
        //cn.Open();
        DataSet ds = new DataSet();

        //SqlDataAdapter da = new SqlDataAdapter("select_grid_sp",cn);
        //da.Fill(ds);
        ds = company.Grid1();
        DetailsView1.DataSource = ds.Tables[0];

         DetailsView1.DataBind();


    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindgrid();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow gd = GridView1.Rows[e.RowIndex];
        company cs = new company();
        //cs.id = Convert .ToInt32 (GridView1.DataKeys[e.RowIndex].Value);

        int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        GridView1.EditIndex = -1;
        cs.delete(i);   
        bindgrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gd = GridView1.Rows[e.RowIndex];
        company cs = new company();
        cs.id  = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

        string companyname = ((TextBox)(gd.Cells[3].Controls[0])).Text;
        string productname = ((TextBox)(gd.Cells[4].Controls[0])).Text;
        cs.companyname = companyname;
        cs.productname = productname;
        cs.update();
        Label1.Visible = true;
        Label1.Text = "update sucessfully";
        GridView1.EditIndex = -1;
         bindgrid();
       
       
    }

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

    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {

    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        bindgrid1();

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindgrid();
    }
    protected void Wizard1_FinishButtonClick1(object sender, WizardNavigationEventArgs e)
    {

    }
    public  override void  VerifyRenderingInServerForm(Control control)
    {
 
    
     }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string attachment = "";
        
            attachment = "attachment; filename= SMSSENDINGREPORTS.xls";
      
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);


        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        HtmlForm frm = new HtmlForm();

        GridView1.Parent.Controls.Add(frm);

        frm.Controls.Add(GridView1);

        frm.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();
    }
    protected void btn_export_Click(object sender, EventArgs e)
    {
        string attachment = "";
        
            attachment = "attachment; filename= SMSSENDINGREPORTS.xls";
   
      

        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);


        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        HtmlForm frm = new HtmlForm();

        GridView1 .Parent.Controls.Add(frm);

        frm.Controls.Add(GridView1 );

        frm.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();

    }
}
//.cs page classfile//
using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.SqlClient;

/// <summary>
/// Summary description for company
/// </summary>
/// 
namespace grid
{
    public class company
    {
        private string company_name;
        private string product_name;
        private int c_id;
        
        public string companyname
        {
            get
            {
                return company_name;
                
            }
            set
            {
                company_name = value;
            }

        }
        public int id
        {
            get
            {
                return c_id ;

            }
            set
            {
                c_id  = value;
            }

        }
        public string productname
        {
            get
            {
                return product_name;
            }
            set
            {
                product_name = value;
            }
        }
        public void add()
        {
            //SqlConnection cn = new SqlConnection(ConfigurationManager .ConnectionStrings [1].ConnectionString );
            SqlConnection cn = new SqlConnection(ConfigurationManager .AppSettings ["cn"]);
            cn.Open();
            SqlCommand cmd = new SqlCommand("insertgridlist_sp", cn);

            cmd.Parameters.AddWithValue("@companyname", company_name);
            cmd.Parameters.AddWithValue("@productname", product_name);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void delete(int id)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
            cn.Open();
            SqlCommand cmd = new SqlCommand("deleterow_sp", cn);

            cmd.Parameters.AddWithValue("@id",id );
            
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public  static DataSet  show()
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
            cn.Open();
            DataSet ds = new DataSet();
            string str = "select_sp";
             SqlCommand cmd = new SqlCommand(str,cn);

             SqlDataAdapter da = new SqlDataAdapter(cmd);
           
            try
            {
                da.Fill(ds);
            }
            catch(Exception  e)
            {
                throw e;
            
            
            }
            return ds;
        }

        public void update()
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
            cn.Open();
            SqlCommand cmd = new SqlCommand("str_company_update",cn);
            cmd.Parameters.AddWithValue("@id",id);
            cmd.Parameters.AddWithValue("@companyname", company_name);
            cmd.Parameters.AddWithValue("@productname", product_name);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public static DataSet show1(int id)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
            cn.Open();
            DataSet ds = new DataSet();
            string str = "select_join_sp";
            SqlCommand cmd = new SqlCommand(str, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.AddWithValue("@id", id);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            

            try
            {
               
                da.Fill(ds);
               
                
                
            }
            catch (Exception e)
            {
                throw e;


            }
            return ds;
        }

        public static DataSet Grid1()
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
            cn.Open();
            DataSet ds = new DataSet();
            string str = "select_grid_sp";
            SqlCommand cmd = new SqlCommand(str, cn);
          

           

            SqlDataAdapter da = new SqlDataAdapter(cmd);


            try
            {

                da.Fill(ds);



            }
            catch (Exception e)
            {
                throw e;


            }
            return ds;
        }
    }
}

No comments:

Post a Comment