Wednesday, January 21, 2015

HTTP Error 404.17 - Not Found (.Net won't run)


You may receive the following error: HTTP Error 404.17 - Not Found.  The requested content appears to be script and will not be served by the static file handler.
This is because .Net isn't configured correctly in IIS.
I ran into this in Windows Server 8 under IIS - Even after installing .Net 3.5 (and hence 2.0) IIS wasn't configured properly - So the static file handler was trying to handle .aspx requests - Resulting in this error.

The fix is simple:
  • Launch Command Prompt - Start - cmd.exe
  • cd  C:\Windows\Microsoft.NET\Framework64\v2.0.50727
  • aspnet_regiis -ir
You should see output like:
Start installing ASP.NET (2.0.50727).
................
Finished installing ASP.NET (2.0.50727).
At this point if you refresh your page it should work properly.
-----------------------------------

Full error text:

HTTP Error 404.17 - Not Found

The requested content appears to be script and will not be served by the static file handler.

Most likely causes:

  • The request matched a wildcard mime map. The request is mapped to the static file handler. If there were different pre-conditions, the request will map to a different handler.

Things you can try:

  • If you want to serve this content as a static file, add an explicit MIME map.

Detailed Error Information:

Module    StaticFileModule
Notification    ExecuteRequestHandler
Handler    StaticFile
Error Code    0x80070032
Requested URL    http://localhost:80/login.aspx
Physical Path    c:\SandBox\Mercurial\wwwroot\Admin\login.aspx
Logon Method    Anonymous
Logon User    Anonymous

Ref

Thursday, January 15, 2015

All about DateTime in ASP.Net and SQL

GridView BoundField Format
<asp:BoundField HeaderText="Post Date" DataField="PostDate" DataFormatString="{0:dd/MM/yyyy hh:mm:ss tt}" />
Result : 16/01/2015 01:59:46 PM


SQL Query for DateTime Range (Normally, Date has no issue)
Eg. You want to query Feb 18, 2014 to Feb 23, 2014

SELECT *
FROM [BinterWebSystem].[dbo].[tblLogHistory]
WHERE LoginTime BETWEEN '2014/02/18 00:00:00.000' AND '2014/02/23 23:59:59.999'
or (+1day to last date, if 2014/02/18, System will assume '2014/02/18 00:00:00.000')
SELECT *
FROM [BinterWebSystem].[dbo].[tblLogHistory]
WHERE LoginTime BETWEEN '2014/02/18' AND '2014/02/24'


Binding all of the Months to DropDownList
System.Globalization.DateTimeFormatInfo dtInfo = new System.Globalization.DateTimeFormatInfo();
  for (int i = 1; i < 13; i++)
  {
      ddlStartMonth.Items.Add(new ListItem(dtInfo.GetMonthName(i), i.ToString()));
  }

Monday, January 12, 2015

Create Loading page in between two pages

Assuming that after login success in Login.aspx, it redirects to Data.aspx which takes 1 minute for loading page. In such case, user will see that Login page will be frozen for that 1 minute loading time. So. the idea is that we need to have one page to interactive with user that data is loading.

>>> Login.aspx

After logged in, redirect to Loading.aspx instead of Data.aspx


>>> Loading.aspx

Add id and runat attribute in body tag
<body id="body" runat="server">
Loading Data...
</body>

Add following code snipped in Page_Load event.
string myScript = @"<script language='javascript' type='text/javascript'>
                                function Redirect() {
                                window.location = '";
        myScript += "Data.aspx";
        myScript += "';}</script>";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Wait", myScript);
        body.Attributes.Add("onload", "Redirect();");

That's it. Enjoy programming. !!!

Thursday, January 8, 2015

Export to excel/csv file download dialog is not prompted if while using ajax Updatepanel

When you have button to export excel or csv file format, saving file will not working if you are using UpdatePanel. It will show following message when you debug.  

Message: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near '

Cause:

The action that causes this code to execute MUST be a postback event, and not an AJAX call.

This is due to the nature of the way AJAX requests are processed.

Solution:

1: Put button outside UpdatePanel (or)

2: Add following code in PageLoad of you aspx page

((ScriptManager)Master.FindControl("ToolkitScriptManagerID")).RegisterPostBackControl(btnExport)


Download csv example

protected void btnExport_Click(object sender, EventArgs e)

    {
        if (ListUser != null)
        {             //Build the CSV file data as a Comma separated string.
            string csv = string.Empty;

            csv = "\"Login ID\",\"Nav Customer No.\",\"Login Time\",\"Logout Time\"";


            //Add new line.

            csv += "\r\n";

            string logouttime;

            foreach (LoginHistory user in ListUser)
            {
                logouttime = user.LogOutTime == null ? "" : Convert.ToDateTime(user.LogOutTime).ToString("dd/MM/yyyy hh:mm:ss tt");
                csv += "\"" + user.UsrName + "\",\"" + user.CustomerCode + "\",\"" + user.Logintime.ToString("dd/MM/yyyy hh:mm:ss tt") + "\",\"" + logouttime + "\"";
                //Add new line.
                csv += "\r\n";
            }

            //Download the CSV file.

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=LoginHistory_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".csv");
            Response.Charset = "";
            Response.ContentType = "application/text";
            Response.Output.Write(csv);
            Response.Flush();
            Response.End();
        }
    }