www.kevincornwell.com

.NET, ASP



.NET DataGrid (or GridView) To Excel Utility
    protected void Button3_Click(object sender, EventArgs e)
    {
 
 
        DataSet dsExport = dataSet;
        DataGrid dgExport = new DataGrid();
        dgExport.DataSource = dsExport;
 
        //ExportToExcel();
 
        DataGridToExcel(dgExport, Response);
    }
 
    protected void DataGridToExcel(DataGrid dGridExport, HttpResponse httpResp)
    {
        httpResp.Clear();
        httpResp.Charset = "";
        httpResp.ContentType = "application/vnd.ms-excel";
        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        DataGrid dGrid = new DataGrid();
        dGrid = dGridExport;
        dGrid.HeaderStyle.Font.Bold = true;
        dGrid.DataBind();
        dGrid.RenderControl(htmlWrite);
        httpResp.Write(stringWrite.ToString());
        httpResp.End();
    }

Or to export a DataGrid to Excel use:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        exportToExcel();
    }
 
    protected void exportToExcel()
    {
        VerifyRenderingInServerForm(GridView1);
 
        /* This is a limit in Excel prior to Office 2007 */
        if (GridView1.Rows.Count > 65536)
        {
            Page.Response.Clear();
            Page.Response.Write("Too many lines to export.");
            Page.Response.End();
        }
        GridView1.AllowPaging = false;
        GridView1.AllowSorting = false;
        GridView1.DataBind();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");
        Response.Charset = "";
        // If you want the option to open the Excel file without saving than
        // comment out the line below
        // Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
 
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

Post Comment Now


ASP.NET – Test NTFS Directory Access
    protected void Page_Load(object sender, EventArgs e)
    {
 
        // Show admin link if they have access to the admin directory. //
 
        hyperLinkAdmin.Visible = hasAccessToDir("admin");
    }
 
    protected bool hasAccessToDir(string dir)
    {
        return System.IO.Directory.Exists(MapPath(dir));
    }

Post Comment Now


How to dynamically add a table row with input fields in .NET.

C# using Visual Studio 2005…

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;
 
public partial class _Default : System.Web.UI.Page 
{
    // How many rows when the page initially loads.
    static int tDefaultRow_Count = 10;
    // Max number of rows.
    static int tMaxRows = 50;
 
    // Do not modify the following.
    private int tUserAddedRowCount = 0;
    static TextBox[] TextBox_Item = new TextBox[tMaxRows];
    static TextBox[] TextBox_Desc = new TextBox[tMaxRows];
    static TextBox[] TextBox_Quantity = new TextBox[tMaxRows];
    static TextBox[] TextBox_Price = new TextBox[tMaxRows];
    static TextBox[] TextBox_Total = new TextBox[tMaxRows];
 
    protected void Page_Load(object sender, EventArgs e)
    {
        tUserAddedRowCount = (ViewState["tUserAddedRowCount"] != null) ? int.Parse(ViewState["tUserAddedRowCount"].ToString()) : 0;
        //Response.Write("User Rows: " + tUserAddedRowCount + "\r\n");
 
        // Recreate the extra rows created by the user on postback
        // events (required to maintain state).
        Add_Table_Rows(tUserAddedRowCount + tDefaultRow_Count);
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
            // User clicked "add a row" button.
 
            Add_Table_Rows(1);
            ViewState["tUserAddedRowCount"] = ++tUserAddedRowCount;
    }
 
    protected void Button2_Click(object sender, EventArgs e)
    {
        // User clicked "Submit" button.
 
        for (int x = 0; x < tDefaultRow_Count + tUserAddedRowCount; x++)
        {
 
            if (TextBox_Item[x].Text.ToString().Length > 0) 
            { 
                Response.Write(TextBox_Item[x].Text.ToString());
                Response.Write(TextBox_Desc[x].Text.ToString());
                Response.Write(TextBox_Quantity[x].Text.ToString());
                Response.Write(TextBox_Price[x].Text.ToString());
                Response.Write(TextBox_Total[x].Text.ToString());
            }
        }
    }
 
    private void Add_Table_Rows(int NumberOfRows)
    {
        for (int x = 0; x < NumberOfRows; x++)
        {
            TableRow myRow = new TableRow();
            Table1.Rows.Add(myRow);
 
            TableCell myCell0 = new TableCell();
            myRow.Cells.Add(myCell0);
            TextBox myTextbox0 = new TextBox();
            TextBox_Item[x] = myTextbox0;
            myCell0.Controls.Add(TextBox_Item[x]);
 
            TableCell myCell1 = new TableCell();
            myRow.Cells.Add(myCell1);
            TextBox myTextbox1 = new TextBox();
            TextBox_Desc[x] = myTextbox1;
            myCell1.Controls.Add(TextBox_Desc[x]);
 
            TableCell myCell2 = new TableCell();
            myRow.Cells.Add(myCell2);
            TextBox myTextbox2 = new TextBox();
            TextBox_Quantity[x] = myTextbox2;
            myCell2.Controls.Add(TextBox_Quantity[x]);
 
            TableCell myCell3 = new TableCell();
            myRow.Cells.Add(myCell3);
            TextBox myTextbox3 = new TextBox();
            TextBox_Price[x] = myTextbox3;
            myCell3.Controls.Add(TextBox_Price[x]);
 
            TableCell myCell4 = new TableCell();
            myRow.Cells.Add(myCell4);
            TextBox myTextbox4 = new TextBox();
            TextBox_Total[x] = myTextbox4;
            myCell4.Controls.Add(TextBox_Total[x]);
 
        }
    }
}

Post Comment Now


Enable .NET Web Extensions In ISS via Command Line

From command line

C:\WINDOWS\Microsoft.NET\Framework\[framework version]\aspnet_regiis.exe -r

After that has run, goto IIS –> Web Service Extensions –> ASP.NET [framework version] –> Enable.


Post Comment Now


ASP.NET Excel Function

This function written in c# builds a MS excel out of a dataSet.

    private DataSet dataSet = new DataSet();
    private DataView dView = new DataView();
 
    private void LoadPeople()
    {
        SqlConnection conn = new SqlConnection("Data Source=asdf;User ID=asdf;Password=asdf;Database=Directory");
        SqlDataAdapter dAdapter = new SqlDataAdapter("SELECT * FROM People", conn);
        dAdapter.Fill(dataSet, "peeps");
        dView.Table = dataSet.Tables["peeps"];
        personel_cnt = dView.Count;
        dView.Table.Columns.Add("is_a_member");
        conn.Close();
    }
 
    private void buildExcelReport()
    {
        DataSet dsExport = dataSet;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw =
           new System.Web.UI.HtmlTextWriter(tw);
        DataGrid dgGrid = new DataGrid();
        dgGrid.DataSource = dsExport;
 
        //Report Header
        hw.WriteLine("My Super Duper Excel Report");
        hw.WriteLine("&lt;br&gt;&mp;nbsp;");
        // Get the HTML for the control.
        dgGrid.HeaderStyle.Font.Bold = true;
        dgGrid.DataBind();
        dgGrid.RenderControl(hw);
 
        // Write the HTML back to the browser.
        Response.ContentType = "application/vnd.ms-excel";
        this.EnableViewState = false;
        Response.Write(tw.ToString());
        Response.End();
    }

Post Comment Now


Blog | Contact | Gallery | Links | Google Latitude (location) | Music | Sandbox | Search | SharePoint | Social Networking | Software | Weather | Web Design


Copyright © 1997-2009 KCSH. All rights reserved.