Saturday, March 26, 2011

Rachit's Archive: Export data from Sql to Excel

Rachit's Archive: Export data from Sql to Excel

Export data from Sql to Excel


In Aspx page:

<asp:Button ID="but1" runat="server" OnClick="click_excel" />

In Cs Page:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class Sqltoexcel : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=.;database=test;uid=test;pwd=test");
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void click_excel(object sender, EventArgs e)
{
String str = "select * from Emp1";
SqlDataAdapter sda = new SqlDataAdapter(str, conn);
sda.Fill(dt);
exporttosql(dt);
}
public void exporttosql(DataTable dtdata)
{
HttpContext context = HttpContext.Current;
string attach = "attachment;filename=example.xls";
context.Response.ClearContent();
context.Response.AddHeader("content-disposition",attach);
context.Response.ContentType="application/ms-excel";
string sep="";
if(dtdata!=null)
{
foreach(DataColumn dc in dtdata.Columns)
{
context.Response.Write(sep + dc.ColumnName);
sep = "\t";
}
context.Response.Write(System.Environment.NewLine);
foreach (DataRow dr in dtdata.Rows)
{
sep = "";
for(int i=0;i<dtdata.Columns.Count;i++)
{
context.Response.Write(sep + "\"" + dr.ToString() + "\"");
sep = "\t";
}
context.Response.Write(System.Environment.NewLine);
}
context.Response.End();
}
labmesg.Text = "datatransfered";
labmesg.Visible = true;
}
}

Code to logof the system

System.Diagnostics.Process.Start("shutdown", "-l -t 00")

Friday, March 11, 2011

asp menu didn't work properly in google chrome

asp menu do not work properly with google crome and Safari . To solve this problem u can put the following function before the page load function in .cs page. the function is

protected override void AddedControl(Control control, int index)
    {
        if (Request.ServerVariables["http_user_agent"].IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) != -1)
            this.Page.ClientTarget = "uplevel";

        base.AddedControl(control, index);
    }


Thanks

Friday, March 4, 2011

Use of ajax

Default.aspx

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <table>
            <tr>
                <td style="width:200px;">
                    Username
                </td>
                <td>
                    <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Password
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    ConfirmPassword
                </td>
                <td>
                    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <table>
                                <tr>
                                    <td style="width:200px;">
                                        State
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddState" runat="server" AutoPostBack="true"
                                            onselectedindexchanged="ddState_SelectedIndexChanged">
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        City
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="ddCity" runat="server">
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="right">
                    <asp:Button ID="cmdSubmit" runat="server" Text="Submit" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



Default.aspx.cs(Code Behind page) 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;

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

          if (!Page.IsPostBack)
        {
            // code to fill the state dropdown list
        }
    }
    protected void ddState_SelectedIndexChanged(object sender, EventArgs e)
    {
        // code to fill the city dropdownlist
    }
}

 

Total Pageviews