Tuesday, December 28, 2010

Dynamic generation of Word(.DOCX) file

Just came across a problem few days back when creating dynamic word(.docx) file. After creation, the com object was still sitting there in the process list, which gave me some weird messages everytime and it always creates the new instance of the word object. I tried a lot and atlast found this solution in MSDN. We need to release the com object finally after completion of our work. Use the below function in your code and call this after you have completed the dynamic generation process.


private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch { }
finally
{
o = null;
}
}


Microsoft.Office.Interop.Word.ApplicationClass oWord = new Microsoft.Office.Interop.Word.ApplicationClass();

*****
Your logic for dynamic generation of word file(in my case)
*****

NAR(oWord);

You are done. Now it should work as expected.

Happy coding..

Labels: , , , , , , , , ,

Friday, November 20, 2009

Visual Studio Error: The project file ' ' has been renamed or is no longer in the solution.

If you ever got this message "Visual Studio Error: The project file ' ' has been renamed or is no longer in the solution." while building a web application, probably you might have added an silverlight project to your solution earlier and removed it.

The cause for this problem is, visual studio removes the silverlight project from your solution when we delete that but the reference it created while adding that project will still exist.

So we have to manually remove the reference to that project.

It will be removed from the project properties window, silverlight projects tab.

Remove the unwanted references related to the projects which you removed from the solution.

Thats it, now you can build your web application.

Happy Coding...

Labels: , , , , , , ,

Friday, October 2, 2009

'The application is already precompiled' error

Iam not sure about the cause for the eror "The application is already precompiled". I faced this error while publishing the website.


The workaround for this error is to delete the 'PrecompiledApp.config' file (also not sure how it is created in the website directory) in your website directory.

Labels: , , , , , , ,

Thursday, October 1, 2009

Server Explorer Gone Invisible?

Is Server explorer on your visual studio gone invisible and throwing the following error when you are trying to view it?

The Visual Studio Explorers and Designers Package ({8D8529D3-625D-4496-8354-3DAD630ECC1B}) did not load because of previous errors. For assistance, contact the package vendor. To attempt to load this package again, type 'devenv /resetskippkgs' at the command prompt.

Follow these steps to get it back.

1.Close all open instances of Visual Studio.

2.Open the Command Prompt in an Administrator context by navigating to Start\All Programs\Accessories, right-clicking Command Prompt and choosing Run as Administrator.

3.Navigate to the install path of VS2008 - in my case (default on Vista x64) it was *C:\Program Files (x86)\Microsoft Visual Studio 2008 9.0\Common7\IDE*

4.Run the command devenv /setup. It takes a little time, wait till it completes.

5.Start Visual Studio.


You have reset your Visual Studio to the default settings through the above steps.

And you can see the Server Explorer again!!

Labels: , , , , ,

Wednesday, September 30, 2009

Month Name from Month Number

Getting Month Name from the Month Number is quite easy by extending DateTime properties in .net.

For Example, if you are having a month number, say 2, which you need to convert it into corresponding Month Name(February)




string strMonth = "2";

DateTime date = new DateTime(1, strMonth, 1);

string strMonthName = date.ToString("MMM");



This will return you the month name "Feb" ,but only first three characters.

If you need the month name in full, slightly modify the above syntax



string strMonth = "2";

DateTime date = new DateTime(1, strMonth, 1);

string strMonthName = date.ToString("MMMM"); // Four Characters will give you the full month name.



Happy Coding..

Labels: , , , , , , ,

Monday, September 28, 2009

Maintain Scroll Position on PostBack in ASP.NET

In order to maintain the page scroll position after the page gets postback, ASP.NET gives us three methods. They are:


1. In Web.config :
<pages maintainScrollPositionOnPostBack="true">

2. In single Page :
<%@ Page MaintainScrollPositionOnPostback="true" ...

3. Programatically :

Page.MaintainScrollPositionOnPostBack = true;

Labels: , , , , , ,

Thursday, September 24, 2009

Offline Mode of your WebSite

A simple .htm file will make your application offline in no time.

Just place App_Offline.htm file on root of your web application, all requests to your web site will be redirected automatically to this file.

Now your Website is completely Offline!

Labels: , , , , , ,

Credit Card Expiration Date DropDownList Sample Code

Below is the sample code for populating Credit Cart Expiration Date DropDownList.


//Populate the credit card expiration month drop down
for (int i = 1; i <= 12; i++) { DateTime month = new DateTime(2000, i, 1); ListItem li = new ListItem(month.ToString("MMM (M)"), month.ToString("MM")); ExpirationDateMonthDropDown.Items.Add(li); } //Populate the credit card expiration year drop down (go out 12 years) for (int i = 0; i <= 11; i++) { String year = (DateTime.Today.Year + i).ToString(); ListItem li = new ListItem(year, year); ExpirationDateYearDropDown.Items.Add(li); }



Happy Coding...

Labels: , , , , , ,

Wednesday, September 23, 2009

String is All Lower or All Upper Case

How do we find a string is All Lower or All Upper case characters?

There are so many ways to accomplish the same thing. We immediately go to the most simple solution; using the ToLower() method on the string object and comparing the original string input to the lower version of the same sentence. The code is shown below.

private bool IsAllLowerCase(string value)
{
return (value.Equals(value.ToLower()));
}

This is a simple method that should be fairly performant. We can make it great performant by using a regular expression. This is shown in the following code:

using System.Text.RegularExpressions;

private bool IsAllLowerCase(string value)
{
// Allow anything but upper case
return new Regex(@"^([^A-Z])+$").IsMatch(value);
}

We can flip around the above regular expression to check for Upper Case.

Happy Coding...

Labels: , , , , , ,

Tuesday, September 22, 2009

Increase the VS screen for HTML pages

By default, when you are in HTML source-editing mode with VS 2008 and Visual Web Developer 2008 Express edition there is a set of drop-downs that are rendered immediately above the HTML text editor view.

This is of no use, when you are using separate code behind files for your page and not lot of inline javascript functions.

We can always hide this navigation dropdown boxes to increase the size of VS screen.

To achieve this, just select the Tools->Options menu item within VS, navigate to the "Text Editor->HTML" node and uncheck the "Navigation Bar" checkbox option.

The navigation toolbar above your html page or html source of your aspx page will be hidden.

You can always enable this by reversing the above process.

After hiding it, if it is not reflecting in your page, try closing and re-opening your aspx/html file.

Labels: , , , , , ,