Saturday, August 13, 2011

Regional settings IIS6 and ASP.Net 2

Issue
1. {0:c} returning too many decimals in gridview
2. {0:d dd/MM/yyyy} returning date with time values
Ex: 12/12/2012 00:00:00

Solution
I solved this problem by installing service pack 2 of ASP.Net 2.0 - after the reboot, the grid worked fine with {0:C} and {0:d}
This problem would have fixed itself in the production environment as it will be set to update automatically. Recently I got the new machine and the firewall seems to be restricting the updates.


Lesson learned: always make sure that all your server software is up to date!


Saturday, June 25, 2011

Compute the total and display it in GridView footer.

There are various way to calculate total in tht GridView Footer. But this method is for simple and easiest way to calculate.

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Collections.Generic"%>
<script runat="server">

private decimal amountSum;

protectedvoid Page_Load(object sender, EventArgs e)
{
List<account> data =new List<account>();
data.Add(new Account("Tom",12002));
data.Add(new Account("Sam", 8900));
data.Add(new Account("Harry", 15558));

gridView.DataSource = data;
gridView.DataBind();
}

string incrementSum(decimal amount)
{
amountSum += amount;
return"";
}

public class Account
{

public Account(string name,decimal amount)
{
this.name = name;
this.amount = amount;
}

privatestring name;
private decimal amount;

public decimal Amount
{
get { return amount; }
set { amount = value; }
}

publicstring Name
{
get { return name; }
set { name = value; }
}

}

</script>
<html>
<head id="Head1" runat="server">
<title>GridView Summary</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gridView"
AutoGenerateColumns="false" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<%# Eval("Amount","{0:c0}") %>
<%# incrementSum((decimal)Eval("Amount")) %>
</ItemTemplate>
<FooterTemplate>
<%# amountSum.ToString("c0") %>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


Wednesday, June 22, 2011

Export a DataTable to Excel in ASP.NET

All spreadsheet applications (Excel, Calc etc.) understand semicolon separated files natively, so everyone can use this method – you don’t even have to use Excel for it to work.

public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();

foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);

foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
}

context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
context.Response.End();
}

Then just call this method and pass the DataTable and the filename as parameters.

ExportToSpreadsheet(table, "products");

The method is static so you can use it anywhere in a web application. Put it on a page, a HTTP Handler, add it to the App_Code folder or stick it in a separate assembly. As long as you call it from a web application it will work.

Tuesday, June 21, 2011

Get current page name from URL

This function can be used to retrieve the current page name from the URL:
name, i.e default.aspx, hello.aspx or whatever.

public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}

By going through System.Web.HttpContext.Current object we are able to have this function in a generic dll or class - and not in each and every page needing to call it.


Tuesday, June 7, 2011

Microsoft Internet Information Services versions 4.0 to 6.0

Open Internet Service Manager or Internet Information Services (IIS) Manager.
If necessary, expand the Web server that you want, and then expand Web Sites.
Right-click the Web site that you want to change.
Click Properties.
Click the Web Site tab.
Change the TCP Port Number in the TCP Port edit box (or click Advanced for multiple Port settings).
Click OK to save the changes.

Microsoft Internet Information Services 7.0

Open Internet Information Services (IIS) Manager.
Select the Web site that you wish to configure.
In the Action pane, click Bindings.
Click Add to add a new site binding, or click Edit to change an existing binding.

Click OK to apply the changes.

Tuesday, May 31, 2011

Make IE7 open pop-ups in a new tab

Tabs are one of the best new features in Internet Explorer 7. I like them because they keep my task bar clean of extra Internet Explorer instances. The only problem I have is with pop-ups that are on my allow list such as when I write a new message in Outlook Web Access.

Follow these steps to configure Internet Explorer 7 to always open up pop-ups in a new tab instead of a new browser window:

1.While IE7 is running, click on Tools and select Internet Options.
2.On the General tab under the Tabs section hit Settings.
3.Under the When a pop-up is encountered section, select Always open pop-ups in a new tab. I also like using the Let Internet Explorer decide how pop-ups should open option because of some web sites I visit.
4.Hit OK and you are finished.


Display Images with IIS7 in Vista or Windows 2008

So, this may seem simple, but for an hour I wrestled with displaying images on IIS7 with vista. ASP.NET worked fine, but no static files, css, jpg’s, gif’s or anything. Just unformatted text.

Turns out when I added the web server in vista, I forgot to check the Static Content checkbox under World Wide Web Services / Common Http Features.

Hope this finds you if you are having the same problem.



















For other non-Vista users, here’s how you find that dialog:

Click Start -> Control Panel -> Programs and Features -> Turn Windows features on or off

Then drill down into IIS as shown in the image above.


Error code 2869 with Vista and Windows 2008

These are the steps to solve the above mentioned problems:

1) Copy the .MSI file to the root directory of your main hard drive (i.e. C:\).
2) Open Windows Notepad.
3) Copy this text into windows notepad:
msiexec /i C:\program_name.msi
(You can write the total path of your .msi file in the system, after msiexec /i.)
4) Replace the text "program_name" in the code that you copied with the actual name of the .MSI file.
5) Click File -> Save As...
Instead of saving it as a .txt file, change the file name to installer.bat.
Save the file to your desktop.
6) On your desktop, right click on the file and select Run as Administrator.


This will run the .msi file properly to install the application in the system.

Sunday, May 29, 2011

Gridview fixed header

<%@ Page Language="VB" AutoEventWireup="False"
EnableSessionState="true" EnableViewState="False" %&gt
<%@ Import Namespace="System.Data" %&gt
<%@ Import Namespace="System.Data.Odbc" %&gt
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt
<script language ="vbscript" runat ="server" &gt
</script&gt
<html xmlns="http://www.w3.org/1999/xhtml" &gt
<head runat="server"&gt
<title&gtUntitled Page</title&gt
<style type ="text/css" &gt
.gridHeader
{
POSITION: relative;
/*IE5+ only*/
top: expression(parentNode.parentNode.parentNode.parentNode.scrollTop-2);
left:expression(parentNode.parentNode.parentNode.parentNode.scrollLeft);
z-index: 99;
}
</style&gt
</head&gt
<body&gt
<form id="form1" runat="server"&gt
<div&gt
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringcountrystate %&gt"
ProviderName="<%$ ConnectionStrings:ConnectionStringcountrystate.ProviderName %&gt"
SelectCommand="select * from gd_master"&gt</asp:SqlDataSource&gt
<asp:Panel ID="Panel1" runat="server" Height="280px" Style="z-index: 100; left: -82px;
overflow: auto; position: absolute; top: 54px" Width="1537px"&gt
<asp:GridView ID="GridView2" runat="server" BackColor="#CCCCCC" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2"
DataSourceID="SqlDataSource1" ForeColor="Black" Height="210px"
Style="z-index: 135; left: 80px; position: absolute; top: -3px" Width="279px"&gt
<FooterStyle BackColor="#CCCCCC" /&gt
<Columns&gt
<asp:CommandField ShowSelectButton="True" /&gt
</Columns&gt
<RowStyle BackColor="White" /&gt
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /&gt
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /&gt
<HeaderStyle BackColor="Black" CssClass="gridHeader" Font-Bold="True" ForeColor="White" /&gt
</asp:GridView&gt
</asp:Panel&gt
</div&gt
</form&gt
</body&gt

</html&gt

Saturday, April 23, 2011

IE 8: X-UA-Compatible doesn´t work?

Add the following line as a
first child of your HEAD element.

< meta http-equiv="X-UA-Compatible" content="IE=7" />