Thursday, November 27, 2008

Microsoft JScript runtime error: 'Sys' is undefined

This is a common issue which occurs often when the web config file is not configured.
To resolve this issue add following to web.config file.

==================================================

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

==================================================

Friday, October 17, 2008

Find n th highest salary

SELECT MIN(SALARY) FROM EMP  WHERE
SALARY IN (SELECT DISTINCT TOP N SALARY FROM EMP ORDER BY SALARY DESC)

Monday, October 13, 2008

Export file to client

Following function downloads specified file from server to client.

=============================================

public static void DownloadFiles(string FileName)
{
HttpContext context = HttpContext.Current;
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
context.Response.ContentType = "application/xml";
string Path = context.Server.MapPath(FileName);

try
{
context.Response.WriteFile(Path);
}
catch (Exception ex) { }
context.Response.Flush();
context.Response.End();
}

Friday, October 3, 2008

Amount validation using Regex and Java Script

I come through a way to validate amount field.
This script validates amount field.

- If the value have a dot, It must be preceded and followed by one or two numerals.

var val = /^-?\d+(\.\d+)?$/.test(document.frm.txt_amount.value);
if(!val)
{
alert("Please enter correct amount.");
return false;
}

Friday, September 12, 2008

Session lost problem after Response.Redirect

If you create a session like this and redirect the user to some other page, the session will lost.

Session["UserId"] = "User1";
Response.Redirect("YourPage.aspx");

This is because of the working of session and Response.Redirect, Lets go through.

" When you create a new session (that is, the first time you write to a Session variable), ASP.NET sets a volatile cookie on the client that contains the session token. On all subsequent requests, and as long as the server session and the client cookie have not expired, ASP.NET can look at this cookie and find the right session.

Now, what Redirect does is to send a special header to the client so that it asks the server for a different page than the one it was waiting for. Server-side, after sending this header, Redirect ends the response. This is a very violent thing to do. Response.End actually stops the execution of the page wherever it is using a ThreadAbortException.
What happens really here is that the session token gets lost in the battle.
There are a few things you can do to solve this problem.

First, in the case of the forms authentication, we already provide a special redirect method: FormsAuthentication.RedirectFromLoginPage. This method is great because, well, it works, and also because it will return the user to the page he was asking for in the first place, and not always default. This means that the user can bookmark protected pages on the site, among other things. " - Bertrand Le Roy

Another thing you can do is use the overloaded version of Redirect:

Response.Redirect("YourPage.aspx", false);

This does not abort the thread and thus conserve the session token. Actually, this overload is used internally by RedirectFromLoginPage.

Friday, September 5, 2008

A very basic functional introduction to Css

Hi,

This is an extreme basic introduction to apply css.

============================

Display <li> horizontally -

<ul class="MyCss">
<li>Asp.net</li>
<li>JavaScript</li>
<li>Css</li>
</ul>

Definition of MyCss
ul.MyCss
{
margin: 0 0 0 0;
}

ul.MyCss li
{
display: inline;
}

===============================

Another way to apply same css -

div ul li
{
display:inline;
}

<div>
<ul>
<li>Asp.net</li>
<li>JavaScript</li>
<li>Css</li>
</ul>
</div>

===============================

Applying css using Tag id -

Lets we have
<span id="mySpan">Hi, css applied</span>

we can define its class using id like this -
#mySpan
{
background: #cfc;
}

===============================

Wednesday, September 3, 2008

How to: Change text of asp:checkbox using java script

Problem : Change text of asp:checkbox when it is checked or unchecked.

Description : Lets we have a asp:checkbox and we need to change the text of asp:checkbox to 'Yes' when it is checked and 'No' when it is unchecked.

Solution :

Lets the checkbox is-

<asp:checkbox id="chkApproved" runat="server">
</asp:checkbox>



We can use getElementsByTagName function from javascript to get asp:checkbox text.

<Script language="javascript" type="text/javascript">
function toggleCheckText(objThis)
{
if (objThis.checked)
objThis.parentNode.getElementsByTagName("label")[0].innerHTML = "Yes";
else
objThis.parentNode.getElementsByTagName("label")[0].innerHTML = "No";
}
</script>

and to call this javascript -

<asp:checkbox id="chkApproved" runat="server" cssclass="normal_txt" onclick="toggleCheckText(this);"></asp:checkbox>

Hope it helps.