Thursday, March 31, 2011


Check out the example below

Save (use notepad) the bolded text below with a .bat extension . Call the .bat file from command prompt.

@echo off

    if not exist "C:\Program Files\MS\myClient" goto INSTALL

msiexec /x myClient.msi /qn

msiexec /i myClient.msi /qn      

Create a desktop URL shortcut from command line

        set WshShell = WScript.CreateObject("WScript.Shell")
        set strDesktop = WshShell.SpecialFolders("Desktop")
         set oUrlLink = WshShell.CreateShortcut(strDesktop & "\MicrosoftLink.lnk")
         oUrlLink.TargetPath = ""

Save the above text in a .vbs file (use notepad) and call the vbs file from command prompt.

To create a shortcut to an exe instead of a web page, check this out

call batch file from batch file

Just use the "call" keyword

eg to do this

call Install1.bat
call Install2.bat
call Install3.bat

How to create a desktop shortcut from command line

   Copy the bolded text below to note pad, customize it to your requirement  and save it with a .vbs extension.
(Customizing to your request means replacing AIM.lnk in the bolded text below with the name you want for your shortcut,  replacing the C:\AIM\AIMStart.exe  with the actual path of your exe file that the shortcut needs to invoke etc.)

        set WshShell = WScript.CreateObject("WScript.Shell")
         strDesktop = WshShell.SpecialFolders("Desktop")
         set oShellLink = WshShell.CreateShortcut(strDesktop & "\AIM.lnk")
         oShellLink.TargetPath ="C:\AIM\AIMStart.exe"
         oShellLink.WindowStyle = 1
         oShellLink.IconLocation = "C:\AIM\aim.ico, 0"
         oShellLink.Description = "AIM"
         oShellLink.WorkingDirectory = strDesktop

After you save the .vbs file,  you can either execute it by double clicking it or invoke it from the command prompt. It will create a shortcut on your desktop. You can also invoke the vbs file thru batch files.

To create a desktop shortcut to a webpage , thru command line, check this out

Tuesday, March 22, 2011

Get current windows user in C# (C sharp)

var user = WindowsIdentity.GetCurrent().Name;

C# convert string to currency

int i = 100000;
//Displays $100,000.00

Get reference to master page in

In this example below assume there is a label with ID= lblMessage in the Master page. Now if you want to access that control in the child page and set its value as "Hello World", this is how you do it.

(Ive used some colors down there so that you can quickly track the child page contents back to the master page.)

Master page code

namespace BugTracker.Master

public partial class BTMasterPage : System.Web.UI.MasterPage



Child page code

using BugTracker.Master;

namespace BugHistory

     public partial class ChildPage: System.Web.UI.Page

            protected void Page_Load(object sender, System.EventArgs e)
               private BTMasterPage PageMaster  = (BTMasterPage)Page.Master;
               lblMessage = (Label)PageMaster.FindControl("lblMessage");
               lblMessage.Text="Hello World";

Further Reading:

How to: Reference ASP.NET Master Page Content

add javascript in code behind

 ddlMyDropDown.Attributes.Add("onclick", "MyJavaScriptFunction()");

Friday, March 18, 2011

There is no source code available for the current location

If you see this error then you need to tell VS where to look for the source code. Let me explain with an example.

Once one of my developers approached me when using enterprise library 5.0. The command he was trying to debug was
The code for that command lies in the enterprise library source code. The screen shots below show how to solve the problem

Solution property Debug Source Files 

Add the directory containing the source code

Just click OK and try debugging again. It will step into those source code files that you just added.

Saturday, March 12, 2011 deployment

How to deploy an web application?

If you are working for the corporate America , you will typically have a development, testing and production phase for every application that you have built. Of the many ways of deploying web applications out there, I find one way for each of these phases, that seems more well suited than the others. Lets go ahead and look into those.

Development server
This is an area where developers enjoy relatively more privileges than the other areas. Assuming you have rights to write onto shared folders on that server,this is how you do it. Share the folder that virtual folder in the IIS points to. Give yourself write permissions to the shared folder. From Visual studio, do what is shown in the images below


2>Type in the shared location

3> Click publish. You are good to go!

Staging or Production Server

This is an area where developers don't have much direct deployment access to. If you could send a deployable to who ever controls that server, that would work out as very convenient. Lets look into on such method. What I will do here is, I will show you a way to create an msi file, which could be either sent directly or with a batch file (to add a little bit more features).

Creating an msi

What you will do here is you will add a new web setup project to your solution. I think images here would make it a lot more quicker to follow

1>Add a new project to your solution

2>Choose Web Setup Project

3>View File system of the web setup project

4>Add project output to web application folder of the web setup file system

5>Add primary output and content files of the web application project

Now just compile the web setup project. You will find the msi file in the debug or the release folder of your websetup project (based on whether your project is set to debug or release).

Using a batch file to force reinstall

To to this, first change the version number of the deployment project to a higher number. (Select the deployment project and hit the F4 key). After you raise the version number,when it prompts you change the product number, click yes. Now generate the msi and use the batch file below to execute it.

Copy paste the bolded text below into notepad and save this file with a .bat extension

@echo off            
myMSIName.msi REINSTALLMODE=amus      >Output.txt      

In the above text replace myMSIName with the actual name of your msi.

Place this .bat file in the same folder as your msi. Then run the .bat file. The bat file runs your msi and forces a reinstall if a version of your website already exists on the server you are deploying to. (It saves you the trouble of uninstalling it manually.)

Further Reading:

ClickOnce Security and Deployment:

ASP.NET Deployment

VS 2010 Web Deployment

Thursday, March 10, 2011

Take sql database offline using query

If you try to take the MS SQL database offline by right clicking and selecting "take offline", it almost always hangs.

Try this instead 

use master
alter database databaseName set  offline with rollback immediate

To bring it back online do this:

use master
alter database databaseName set  online

validate email address

How to validate an email address in

To validate an email address in, use the RegularExpressionValidator. In the validate expression property of the RegularExpressionValidator, select internet email address.

This is how the aspx code would look like

        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
            ErrorMessage="Please enter a valid email address" ControlToValidate="txtEmail"

If you are a beginner, look at the screen shots below