Loading jQuery from Microsoft’ CDN

By chris on Feb 23 2010 | 1 Comments

Loading commonly used jQuery files from CDN is always appreciated. Loading these files from a CDN improves the page loading speeds in a variety of ways.

1. When using a CDN,the files we upload, are distributed over a large amount of server on different locations. The CDN automatically fetches the content from the server closest to the user’s location and gives it to the client.

2. Pages load faster it you can distribute content over different domain/sub domains. browsers can only handle up to a certain number of connection per domain, so when we request a CDN file, it make requests in parallel, making it advantageous.

3. All js files loaded from Microsoft’s CDN are cached, so every time you do a request, its might be loaded from your browser cache. This does save requests and in turn bandwidth.

The following code shows how to get a jquery 1.3.2 minified js file from Microsoft’s CDN. It also shows how you can specify a fallback js file from the local machine in case the CDN is not working.

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
{
    document.write(unescape("%3Cscript src='/Scripts/jquery-1.3.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>

For more information, please visit http://weblogs.asp.net/jgalloway/archive/2010/01/21/using-cdn-hosted-jquery-with-a-local-fall-back-copy.aspx and for Microsoft's CDN visit http://www.asp.net/ajaxLibrary/cdn.ashx

There is also a Google CDN for js files, but it requires you to have a Google API key. More details can be found at http://code.google.com/apis/ajaxlibs/documentation/

Post info

Categories: HTML , JavaScript
Bookmark and Share

In clause and dynamic sql query

By chris on Jan 27 2010 | 0 Comments

All of you people who might have tried to write IN conditions in dynamic sql know that it’s not a pretty easy task. If the IN clause comes into the stored procedure as a parameter, you will need to write the whole sql into a string, append the in clause, and then execute it using the EXEC command. But this again fails if the string is too long and you end up going crazy. But there are workarounds for this; but it is not recommended.

There is a good tutorial blog post by Erland Sommarskog, which can be seen at the following link, http://www.sommarskog.se/dynamic_sql.html. In this post, he gives detailed information on why people should avoid using dynamic sql, and also gives workarounds for issues we face when writing dynamic sql.

Post info

Categories: MS SQL
Bookmark and Share

Show alert from postback inside asp:UpdatePanel

By chris on Jan 24 2010 | 2 Comments

Everyone might have a situation when they might need to show and message or an alert box based on some server-side conditions. Usually people do a Response.Write(), with javascript code in it. But there is a more easier method than this. It is by using a method called ClientScript.RegisterStartupScript.

Below is the code segment.

string Message = "Hello World!"; ClientScript.RegisterStartupScript(this.GetType(), "alert", string.Format("alert('{0}');", Message), true); 

If you are trying this from within a update panel, ClientScript.RegisterStartupScript will not work. In this case, you will need to use the method ScriptManager.RegisterStartupScript.

Below is the code you can use to do it.

string Message = "Hello World!"; ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", string.Format("alert('{0}');", Message), true); 

This is will help you to create an alert message box on postback of a submit button from server side.

Post info

Categories: ASP.NET , C# , JavaScript
Bookmark and Share

Generating sql insert scripts

By chris on Dec 15 2009 | 0 Comments

It is always a difficult task for migrating data from one database to another. There are pretty much amount of softwares available in the market for this purpose, but most of them are not freeware software. Most of them have a trial version, but they will have certain limitations like, number of inserts generated, data types converted etc.

I was in a need for such a software and at last found out one piece of code, actually a stored procedure, which does this functionality. Credits to Vyas (http://vyaskn.tripod.com/), this stored procedure generates sql insert queries for a table. Just create this stored procedure in your db and pass the table name as parameter and it just generates the queries corresponding your data in seconds. You can use these insert queries to insert data in your destination database.

You can get the stored procedure from the following link http://vyaskn.tripod.com/code/generate_inserts_2005.txt

For anyone who still uses SQL SERVER 2000, you got a version for 2000 which is available at http://vyaskn.tripod.com/code/generate_inserts.txt

Hope this helps!

Post info

Categories: MS SQL
Bookmark and Share

Session terminating unexpectedly

By chris on Dec 13 2009 | 0 Comments

I was working on a website for one of my clients last day. When I was testing site, and I was getting an error “Object reference not set to an instance of an object”. I immediately got into the rescue. I started digging into the stack trace to find the clues about the error. After going thru it, I could find that the issue was caused when I tried to access a Session variable. ASP.NET was saying that the session variable was not set and thus threw the exception. But I was damn sure that I set the session variable before accessing it.   OK.   I exited the application and ran it again, and to my surprise it worked fine, but now, the same error started throwing at a different place in the code. If I refresh the page, it works fine. Each and every time I run the application, Session errors popped up at random places and in no pattern. This started driving me nuts. I was determined to find out the solution for this.

I goggled out for solutions and after much time, I could find out the problem was due to the IIS Settings in the Windows 2003 server.

What was happening is that, my website’s, application pool was configured as a web garden. This is done to improve the performance of a web site. What is does is that it allocates more number of worker process to handle the requests and hence it improves performance. But the downside of this is issues with Session.

By default, Session works in the InProc mode. Web gardens does not support session in InProc mode. So the solutions for this is either to set the number of worker processes in the IIS to1 or to change the Session Mode to use Sql Server.

To check weather your website uses a web garden

1) Open IIS

2)  Open Properties of your website.

3) Check which Application pool is used by the website

4) Open Application pools in the right side pane of IIS

5) Right click and open properties of the Application Pool.

6) Change the no of worker process and its done!

 

Post info

Categories: ASP.NET , C#
Bookmark and Share

Css selector compatibility tester

By chris on Aug 05 2009 | 0 Comments

It is a difficult task converting psd to html, and making it into html that cross browser compatible is much more difficult. You will have to create your css and make sure it works in all the browsers including IE6, IE7, Mozilla, Chrome, Safari etc.

You will start writing the css and after sometime you will notice that some of the css selectors work in some browsers, and not in other browsers and you will have the hectic task of rewriting it again.

So even before you start writing css, run the CSS Selector tester. Try to write the css with selectors that you will find working in all the target browsers. this will help you in reducing the time to make the css cross browser compatible.

 

The css selector test suite is available online at http://www.css3.info/selectors-test/

Post info

Categories: CSS , HTML
Bookmark and Share

Exclusive access error while restoring database

By chris on Jul 22 2009 | 1 Comments

I was trying to restore a database backup on to an existing database in MS SQL 2005.

The error was

System.Data.SqlClient.SqlError: Exclusive access could not be obtained because the database is in use. (Microsoft.SqlServer.Express.Smo)

 

 

 

I googled around it and I found many ways to get around it. The cause of the error is that the database is still being used in some connection.SQL needs exclusive access to restore the database. So the easiet method in my opinion will be to restart the MS SQL and restoring it. You can restart the MS SQL from either SQL Server Management Studio or by going to Services in the Control Panel.

Post info

Categories: MS SQL
Bookmark and Share

trim, trim left and trim right with character as parameters in JavaScript

By chris on Jun 17 2009 | 0 Comments

Everyone might be aware of the whitespace trim functions in Javascript. But I had to google a lot to find out the trim functions which take in a character as parameter and trim out those characters from the input string. And here are the functions I have put together for your ease. I have also added a method you can use to find out how the code works.

   <script language="javascript" type="text/javascript">
    
    String.prototype.trim=function()
    {
        return this.replace(/^\s+|\s+$/g,"")
    };

    String.prototype.trimStart=function()
    {
        return this.replace(/\s+$/,"")
    };

    String.prototype.trimEnd=function()
    {
        return this.replace(/^\s+/,"")
    };

    function trim(str, chars) 
    {
	    return ltrim(rtrim(str, chars), chars);
    }
     
    function ltrim(str, chars) 
    {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }
     
    function rtrim(str, chars) 
    {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    }
    
    function test()
    {
        var text="   asabbbascccaas   ";
        alert("-"+text.trim()+"-");
        alert("-"+text.trimStart()+"-");
        alert("-"+text.trimEnd()+"-");
        alert("-"+trim(text.trim(),'as')+"-");
        alert("-"+ltrim(text.trim(),'as')+"-");
        alert("-"+rtrim(text.trim(),'as')+"-");
    }
    </script>

 

Source: ASP.NET AJAX Reference and Webtoolkit

Post info

Categories: JavaScript
Bookmark and Share

innerText equivalent for FireFox

By chris on Jun 02 2009 | 0 Comments

I had the opportunity to work in a Javascript and HTML only project recently. The project was already done, and my part in the project was to ensure that the website has no cross browser issues. The programmer who had developed the system only ensured that the application worked in IE, so I had a difficult time ahead to make it functional in all modern browsers.

One of the main mistakes by that programmer, was the use of innerText property for handling text. Unfortunately this property doesn't work in Firefox. The work around I planned was to get the innerHTML of the element and then later stripping out the HTML to get the text.

I used the following method to strip of HTML from text in JavaScript

String.prototype.stripHTML = function()
    {
        var matchTag = /<(?:.|\s)*?>/g;
        var str=this.replace(matchTag, "");
        return str;
    };

 

But it was only later that I understood that there was a pretty way ahead instead of the above method. It was by using the property called textContent. It returns the text content of the node and its child nodes.

document.getElementById(element1).textContent=str;

 

This property works out in all browsers except IE. More details can be found out from the following link http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent

 

UPDATE (09 July 09): I found that the above stripHTML function does not remove &nbsp; or convert it into spaces. You can change the method like this to accomplish the same.

String.prototype.stripHTML = function()
    {
        var matchTag = /<(?:.|\s)*?>/g;
        var str=this.replace(matchTag, "");
        str=str.replace(/&nbsp;/g,' ');
        return str;
    };

Post info

Categories: HTML , JavaScript
Bookmark and Share

Essential tools for ASP.NET developer

By chris on May 22 2009 | 1 Comments

Recently I bought my work horse. It’s a Dell Inspiron 1525.

As soon as I got it, I started to install programs for ASP.NET web development, and here is my checklist of programs for an Asp.Net programmer...

Essential tools-

1) Visual Studio Express

2) SQL Server Express

3) SQL Management Studio

4) IIS

5) ASP.NET Ajax Extensions

6) Ajax Control Toolkit

7) Firefox

8) Fire bug (Firefox Addon)

9) IE Developer Toolbar

 

Other important softwares

1) CCleaner

2) Teamviewer

3) Skype

4) Firecookie (Firefox Addon)

5) ScrapBook (Firefox)

These are my list of essentials; please do post the ones you use.

 

UPDATE (22/07/09): I am adding WinMerge to the list of other softwares that a developer must have. It is a great tool to compare and merge different versions of the same file. This will be particularly useful for people who usually integrate other's work.

Post info

Categories: General
Bookmark and Share