Friday, November 21, 2008

Parser Error Message: Access to the path ... (ASP.NET)

I have been working on an ASP.NET page and all of a sudden, I was getting Parse Error on this page as shown below:

All I did was modify the code in the page and plus I could build the page in Visual Studio 2008 with out any errors. I could also build the entire web site with out any errors, but I was still getting the error.

I compared the file with the previous version and I do not see any special characters or anything like that. I even tried removing the using statements and putting them back again thinking there may be some hidden character on the first line, but still did not help.

I finally, made a copy of that file and got the previous version from the Source Control and then the error is gone. I then deleted the previous version and renamed the file (that was giving the problem) to its original name and the error is still gone.

Bottom line, I do not know what caused it and neither do I know how doing what I just did fixed the problem, but thought putting it here would might come handy for anyone that run into this issue.

After I did this, I also found this blog post and the author of this post had to reset the permissions of the file that was causing this error.

Have fun coding.
Sonny

Saturday, November 1, 2008

Using AJAX 3.5 toolkit with latest version of SharpForge 0.5.14b

I just installed the SharpForge version 0.5.14b on my system that has .NET Framework 3.5 and Visual Studio 2008 + Visual Studio SP1. That is, I have AJAX toolkit version 3.5 running and when I browse to the SharpForge web page, I am getting the following error from web.config:

I tried changing the version # for System.Web.Extensions to 3.5.0.0 everywhere it is referenced in the web.config but I am still getting this version error for other user controls in the app. I searched on the SharpForge site and all it indicated is that you would have to install AJAX 1.1 toolkit and I do not want to install the older version of AJAX along with the latest version.
I finally found few pages on the web that talk about how you can run old web sites that are built using AJAX 1.1 toolkit with the latest version with out having to install the older AJAX toolkit. You basically tell the app to use 3.5 version of System.Web.Extensions every time it needs the older version of System.Web.Extension using runtime section.
Here is a blog post that talks about it:
HOWTO: Running ASP.NET 2.0 Ajax Toolkit 1.0.x in .NET 3.5 / SP1 IIS

I spent quite a bit of time chasing this issue and I hope other people find this solution sooner.

Thanks.

Sonny

Wednesday, May 14, 2008

Check/Validate a date string in Javascript


I was looking for a simple Javascript function to check or validate a date string and most of the examples I have come across are too verbose. So I came up with a simple function that uses Regular Expressions and the built-in Date class.
//Checks if a given date string is in 
// one of the valid formats:
// a) M/D/YYYY format
// b) M-D-YYYY format
// c) M.D.YYYY format
// d) M_D_YYYY format
function isDate(s)
{   
  // make sure it is in the expected format
  if (s.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)
     return false;

  // remove other separators that are not valid with the Date class    
  s = s.replace(/[\-|\.|_]/g, "/"); 

  // convert it into a date instance
  var dt = new Date(Date.parse(s));     

  // check the components of the date
  // since Date instance automatically rolls over each component
  var arrDateParts = s.split("/");
     return (
         dt.getMonth() == arrDateParts[0]-1 &&
         dt.getDate() == arrDateParts[1] &&
         dt.getFullYear() == arrDateParts[2]
     );   
}
 
// test function to test the isDate function
function test_isDate()
{
  var arrDates = [ 
   '05/15/2008', '05-15-2008', 
   '05.15.2008', '05_15_2008',
   '15/15/2008', '05/35/2008',
   '05/15/1908', '15-15-2008',
   'a/1b/2008', '05/30/a008' ];
  for (var d in arrDates) 
     document.writeln("isDate('" + arrDates[d] + "') : " + isDate(arrDates[d]) + "");
} 
By the way, I am using a Javascript utility called SyntaxHighlighter, to render this code. If you have not already used this utility, I would highly recommend you check it out.

The code is self explanatory and when you run the test function, you get the following results:

Hope you find this useful. Please let me know if you come across any other elegant and efficient ways to check if a given string is a valid date or not using Javascript.

Happy coding,
Sonny

Quickly see Document and Window properties using Javascript

In my last post View dynamically generated HTML, I talked about how you can add a button to your Links toolbar in IE and link it to a snippet of Javascript code.

Here are a couple of snippets that I use to examine the current document properties and window properties. I have added them as "Show Doc Props" and "Show Win Props" buttons to my IE Links toolbar and they come very handy while debugging my web pages. These can also be used to look at all the different properties of document/window instance of any website that you visit:

Snippet for "Show Doc Props"
javascript:var dprop_=new Function("propName","try{return document[propName];}catch(err){return '<span style=color:red;>'+err.description+'</span>';}");var tbl="<table border='1' style='font-family:Tahoma;'>";for(var p in document)tbl+="<tr><td style='color:blue;'>"+p+"</td><td> "+dprop_([p])+"</td></tr>";tbl+="</table>";window.open("").document.write(tbl);


Snippet for "Show Win Props"
javascript:var wprop_=new Function("propName","try{return window[propName];}catch(err){return '<span style=color:red;>'+err.description+'</span>';}");var tbl="<table border='1' style='font-family:Tahoma;'>";for(var p in window)tbl+="<tr><td style='color:blue;'>"+p+"</td><td> "+wprop_([p])+"</td></tr>";tbl+="</table>";window.open("").document.write(tbl);

As you can see, this technique of linking code snippets to a button in your IE toolbar is a great way to access some general purpose Javascript utilities. One thing that I can think of is to have different set of snippets to automatically fill-in different form data. I understand that IE provides the abilitiy to save form data, but I do not like that feature plus it is not very flexible. For example, what if you want to use a different set of data based on the form or web page you are on? You could definitely use this technique for that purpose.

Hope you find this useful. Happy Scripting,


Sonny

Monday, May 12, 2008

View dynamically generated HTML in IE


Have you ever wanted to see the dynamically generated HTML of a page? If you use AJAX or lot of Javascript, I am sure you have added HTML content to your page on the fly and you wanted to see the active HTML source of that page, since IE only shows the original HTML source.

Perhaps you already know about how to do this, but, I often use this little Javascript code snippet to see the active content of a page and it really comes handy while developing web pages that use lot of Javascript to dynamically add content:

javascript:
window.open("").document.open("text/plain", "").write(document.documentElement.outerHTML);

I simply type this in the address bar and viola, I have the current HTML source of the page popped up in a new browser window. Better yet, I created a bookmark link using this snippet and every time I need to see the active HTML source of a page, I just click on this link.

Here are the steps to add a bookmark link for this Javascript snippet:
  1. In IE, press Ctrl+D to bring up "Add a Favorite" dialog
  2. Give it a name, something like "View Active Source"
  3. Use Links folder to create in and OK the dialog
  4. Have the Links toolbar displayed in your browser if it is not already
  5. Right-click on the "View Active Source" button > Properties
  6. In the "Web Document" tab, type the above line "javascript:..." in the URL box and OK the "View Active Source Properties" dialog
  7. Click "Yes" to the following warning message:
    The protocol "javascript" does not have a registered program. Do you want to keep this target anyway?
  8. Now, click on the button "View Active Source" in the Links toolbar and now you should be able to see the active HTML of the current page in a new window
There are few gotchas that you should be aware of as listed below. Use this snippet at your own risk and be warned that getting same Javascript code to consistently work across multiple versions of IEs is like peeling onions:
  1. As my title indicates, I have only tested this in IE and do not know if it works in other browsers or not. On that thought, other browsers might already provide this functionality and I remember hearing from a friend that Firefox does
  2. It won't work if Popup Blocker is turned on
Hope you find this useful and if you know any other cool ways to see the active HTML content of the page, please let me know.

Thanks for visiting my blog.


Sonny

Friday, May 9, 2008

Convert an integer to an ascii character in Javascript #2


In my previous post titled Convert an integer to an ascii character in Javascript, I said, I could not find any built-in methods in Javascript to convert a char into an integer and vice versa, well, it turns out that I have not looked hard enough. There are two methods on (one static and one instance as expected) String class that would do this conversion and these method are available since Javascript 1.2. I am sorry about the overlook. Here are sample functions to illustrate how these methods can be used in your code.

// Converts an integer (unicode value) to a char
function itoa(i)
{
   return String.fromCharCode(i);
}


// Converts a char into to an integer (unicode value)
function atoi(a)
{
   return a.charCodeAt();
}

Thanks,


Sonny

Wednesday, May 7, 2008

Convert an integer to an ascii character in Javascript


See my 2nd post for the revised code using built-in Javascript methods.

I was looking for a function to convert an integer into an ascii character in Javascript and could not find one. This is because Javascript does not support explicit character types, and to Javascript character is nothing but a string of length 1. This is what I have come up with. First, you define a string constant consists of all the ascii-7 characters. For non-printable characters you simply use a "?":

//reference: http://www.asciitable.com/
var asciiTable =
// 0 thru 9
"??????????" +
// 10 thru 19
"??????????" +
// 20 thru 29
"??????????" +
// 30 thru 39
"?? !\"#$%&'" +
// 40 thru 49
"()*+,-./01" +
// 50 thru 59
"23456789:;" +
// 60 thru 69
"<=>?@ABCDE" +
// 70 thru 79
"FGHIJKLMNO" +
// 80 thru 89
"PQRSTUVWXY" +
// 90 thru 99
"Z[\\]^_`abc" +
// 100 thru 109
"defghijklm" +
// 110 thru 119
"nopqrstuvw" +
// 120 thru 127
"xyz{|}~?";

Then you could have a function like this:
function itoa(i)
{
   return asciiTable.substr(i, 1);
}

Similarly, for printable characters you can have a function like this to convert character into an integer:
function atoi(c)
{
   return asciiTable.indexOf(c);
}

Hope you find this useful, and please let me know if you know any other elegant or efficient ways to do this in Javascript.

Good scripting,

Sonny

Wednesday, April 23, 2008

Extract URLs from Internet Explorer(IE) History cache


The other day I was trying to extract bookmarks from my IE's history cache, but could not find any tools in IE to do that. Looked around on Google and found a VB Project, which is fine, but I am looking for something using C#. Then, I found this article on CodeProject titled The tiny wrapper class for URL history interface in C#, which would work, but I want a simple script that I can tweak and just run from the command prompt. Again, it would be nice if it is in C# as I have been using this C# Script tool for a while and I really love it. It is called The C# Script Engine by Oleg Shilo and I would highly recommend you explore it if you are not already aware of this gem. It is really clean and lean and does not require any special syntax or setup and could simply use a file that was created with Visual Studio. If you want to automate something and want to do it in C#, this is the tool to use. I would have more to write about this tool later.

Back to our original task, searching on the API calls that are referenced in the above articles, I found the following two links (one on MSDN and one on Google's Code project) which looked promising:

HOW TO: Clear the Cache When Your Application Hosts a WebBrowser Control in Visual C# .NET

WinApis.cs file on Google Code project called csexwb2 - The most complete .NET Webbrowser control wrapper

Mostly based on the code given in WinApis.cs, I written a C# Script that can be run using the C# Script Engine mentioned above or can also be run using csc.exe that comes with the .NET Framework. You could also add this to any C# IDE Project file and run it from there. It writes the entries from IE's cache to a text file called "ieHistory.txt".

The code in the above script file is pretty self explanatory, but I would like to mention a few things that might come handy if you like to experiment with or tweak the script more.

On the MSDN page, it declares the lpszSourceUrlName and lpszLocalFileName as IntPtr and I had problem converting these fields to Strings, so I used the struct definition given in csexwb2.

Using the PInvoke declarations on MSDN page, I was not able to get it recognize the urlPattern argument, and I had to use the declarations given in csexwb2.

Hope you find this useful.

Best Wishes,

Sonny

Tuesday, April 15, 2008

Move VirtualPC Base & Differencing Disks

Have you ever wanted to move your Virtual PC base and differencing disks to a different locations or to a different system?  Well, I recently have. A friend of mine sent me the following blog post (I highly recommend it if you are thinking about using virtual systems), which talks about how to setup virtual systems using differencing disks:

Use VirtualPCs Differencing Disks To Your Advantage

As per this post, I have created the following three virtual disks:
Virtual Disk Description
base_WinXPsp2.vhd* This is the base disk that containsthe Windows XP with SP2.
diffOnBase_VS2K5.vhd* This is the diff disk on top ofthe base disk and it has Visual Studio 2005 installed.
sandbox1.vhd This is the final diff disk on top of the VS2K5 diff disk.

* Reusable virtual disks.

The idea here is to reuse the virtual disks base_WinXPsp2 and diffOnBase_VS2K5 again and again and have different configurations (or sandboxes) built on top of these.  You do not want to change these virtual disks and hence you would make them read-only.

As this is the first time I started using the Virtual PCs for my daily work(development), I have not given much thought to how I should organize the files and how I can leverage the base images.  I have kept all of these files in the same directory and started using this as my main development machine.  If I need another virtual system, I thought, I would simply copy the reusable disks into a new folder and then create new virtual disk on top of that (i.e. sandbox2.vhd, sandbox3.vhd ... sandboxN.vhd so on) and start using it.

The main reason behind using reusable virtual disks is to save space when you create many virtual systems.  Obviously, I am not leveraging that benefit here. So, I have rearranged them as shown below:



After I made sure the base virtual disk file (base_WinXPsp2.vhd) is read only, I used the Virtual Disk wizard and changed the parent virtual disk references. Here are the steps that I have used:
1) Make sure base_WinXPsp2.vhd is read only.  
2) Make sure diffOnBase_VS2K5.vhd is not read only since we will be changing the parent virtual disk reference stored inside this file.  
3) Start the Virtual Disk Wizard.
4) Select the "Edit an existing virtual disk" option.
5) Select the current working image's virtual disk (sandbox1.vhd) and click "Next".
6) Change the path to parent virtual disk (i.e. diffOnBase_VS2K5.vhd) of sandbox1.vhd.
7) Change the path to parent virtual disk (i.e. base_WinXPsp2.vhd) of diffOnBase_VS2K5.vhd.
8) You might get a warning about modifying the parent virtual disks, hit OK on this dialog.
9) Finally, you get a dialog about asking you to merge virtual hard disks.  Since we are not interested in merging any disks, we just wanted to change the reference paths stored inside the disks, so go ahead and click on the “Cancel” button. 
This is the key step that is not very obvious that we could skip this part.

Now, go ahead and double click on your virtual system configuration file (i.e. sandbox1.vmc) and it should successfully start the virtual system.  You can create more systems (sandboxes) off of the reusable virtual disks as I have done with sandbox2 and sandbox3.

Virtual Cheers,

Sonny :)

Friday, April 11, 2008

Snapshot of active window in Virtual PC

Lately, I have been doing most of my work using a Virtual PC and I often find myself having to take a snapshot of the active window (dialog box), especially to create all my blog entries.

I know that Alt+Print Screen copies the snapshot of the active window to the clip board, but in Virtual PC environment RightAlt key acts as the Host key, so if you use the RightAlt key it would not work. So make sure you always use the LeftAlt key to take a snapshot of the active window in Virtual PC environment.

hostname works but localhost and local loop back address do not work in IE

I recently ran into a strange issue and spent about a day troubleshooting this so I thought I share my findings here.

I had a classic ASP webapp running on a virtual system and I could access it using http://localhost/webapp/ or http://127.0.0.1/webapp URLs. No problem, until I made the following changes:

  1. Added the virtual system to our domain to be able to access some apps

  2. Moved the virtual system files to an external HDD

I could no longer access the app using either one of above URLs. When I tried to access the app or even an image file off of this web app using http://localhost/webapp/, I get a "DNS Fail" error message as shown below:


And if I tried to access the app using http://127.0.0.1/webapp, I get a "Access Denied" error message as shown below:

I am not sure which one of these is caused this issue or if the above changes are even the culprits.

I tried the usual stuff (not necessarily in the order listed below) like the following:

  1. Checked my C:\WINDOWS\SYSTEM32\drivers\etc\hosts file and made sure local loop back address is mapped to localhost.

  2. Pinged the localhost to make sure it is getting resolved correctly.

  3. Checked the IIS configuration to make sure the "IP Address" is set to "All Unassigned" in the "Web Site" tab:


  4. I have checked the permissions on the folder mapped to the Home Directory.

  5. I have un-installed and re-installed IIS.

  6. Followed the steps given in the Microsoft KB Article ID: 271071 (How to set required NTFS permissions and user rights for an IIS 5.0 Web server). CAUTION: This process took a long time to complete.


After trying few other things that I could not quite recall all the details, I wondered if I should access the webapp using the hostname, i.e. http://myhost/webapp and bingo! it worked. I am still not sure why hostname works and the other two don't. I searched using Google and I did not find any similar issues, so I do not know what is causing it.

I gave up for the day and decided to leave it like that. The next day, I wanted to give it another try since it was really bothering me.

I decided to use the telnet command to fetch the starting page using all three URLs and lo and behold all of them retrieved the right content. So, there is nothing in the IIS that is causing this, so some setting in Internet Explorer (IE) must be causing it. I tweaked a few settings in IE and they did not fix the problem. Then I found the reset all button.

I reset all the IE settings and restarted IE and now I could access my app using all three URLs.

So, I still do not know what caused this strange issue, but am happy that it is fixed now. I guess, if it happens again I know what to try and where to look.

Hope you find this useful.

Sonny

Friday, March 28, 2008

PowerPoint Viewer cannot open the file


Here is my recent Saga trying to view Microsoft PowerPoint 2007 file saved in Office Open XML format, i.e. pptx.


Prelude:

I work in the Windows world like many others out there and believe in Microsoft products and technologies to most extent, but recent experience in trying to view a pptx file shaken that belief quite a bit. I only have Office 2003 installed on my system and I received a pptx file and needed to view this pptx file.


Journey:

To view this file on my system, I had to jump through few hoops and spend quite a bit of time. Here is my journey (mostly missteps) and hopefully will save you sometime if you run into the same situation:


1) Thinking it would be a simple process of getting the right viewer, I obtained the PowerPoint Viewer from Microsoft website and installed it and then tried to open the file. To my surprise, I get the error "PowerPoint Viewer cannot open the file" ...:



So, I have searched on the web and found the following posting:
http://www.eggheadcafe.com/software/aspnet/31691910/cannot-open-in-viewer.aspx


According to the Lucy Thomson (PowerPoint MVP) who posted a reply to the original question on this page, 2007 Viewer basically is 2003 Viewer combined with converter that converts 2007 files to 2003 format. I unzipped the contents of the pptx file and everything looks fine as far as its contents. So, there must be something wrong with the converter packaged in that viewer.


2) I emailed the file to my gmail account to see if it would display it there, no luck there also. Gmail only supports ppt files.


3) I searched some more and found this tool called Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats. Bingo, now I can open the pptx file using PowerPoint 2003. I am kind of relieved but this is not the end of my saga by any means, read on and you see why.


4) Meanwhile, i.e. while I was searching on Google, I emailed a colleague to see if he was able to open this file on his system. I got the reply saying he was able to open the file but it took longer to open it. He did not give any more details. So, I did little more probing:

  • He was also using Office 2003

  • Did not have to install any software (I thought he must have had to install some addon or something, since the only way I could view the file is by installing Compatibility Pack)

  • Running Office 2003 SP2 (I am also running SP2)


Got me puzzled, then I decided to check his actual version/build # and it turns out that it is slightly higher than my version # even though we both running SP2.


4) So, I thought I would also update my Office 2003 and then I should be able to open the pptx file without any problem. Since I do not like to have any extra software on my system I uninstalled the Compatibility Pack thinking I do not need it.


Now, I updated my Office 2003 to SP3 (that is the only office update choice I had through Windows Update). I figured, it should be OK since I will be getting the latest and greates updates to Office 2003 and I should have no problem opening the pptx files.


So, after I updated to Office 2003 SP3, I tried to open the pptx file and I get the following message asking me if I want to install Compatibility Pack:



I am back to where I started. I hit "No" since I already had the Compatibility Pack downloaded. I installed the "Compatibility Pack" again and now I can open the pptx files.


5) As I have mentioned earlier, I do not like to have any software that I really do not need, so I have uninstalled the Viewer and I could still open the pptx files with my Office 2003 (thanks to Compatibility Pack).


6) You do not necessarily have to have Office 2003 to use the Compatibility Pack, you could also use it with the PowerPoint 2003 Viewer. So, I have installed PowerPoint 2003 Viewer and the Compatibility Pack on a Virtual Machine to see if I can open pptx file using the 2003 viewer and sure enough I could view it.

Lessons Learned

Do not use the PowerPoint Viewer 2007. It is worthless.


If you have Office 2003 installed, then to view/open the pptx files, simply install the Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats.


If you do not have Office 2003, then to view the pptx files, first install the PowerPoint 2003 Viewer and then followed by Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats.



Happy PowerPoint Viewing ...


-Sonny