Wednesday, October 24, 2012

How to get client machine Outlook username using Asp.Net ?


If we want to get server Outlook user name then it is easy to get because for this we can simply use “Microsoft.Office.Interop.Outlook” Namespace, and after creating the object of “Microsoft.Office.Interop.Outlook.Application“ Class we can get server Outlook user name.

But we face the problem when we want to get Client side (Client machine) Outlook user name, because “Microsoft.Office.Interop.Outlook” Namespace targets to server side machine. In this article, I am explaining - How to get client machine Outlook username?

As we want to get client machine Outlook user name so we have to write code which will target to client machine, so I have used JavaScript for this. Using JavaScript I am dealing with ActiveX object so it is must that our browser should allow ActiveX control.

For this we need to set following setting for our browser-
1. Open Internet Explorer
2. Go to "Internet Options Settings"
3. Open "Security Tab"
4. Mark as enable for "Initialize and script ActiveX controls option"

Like this-



Internet options


This setting will help to deal with client machine Outlook object.

Now use following code -

 protected void btnGetOutlookUser_Click(object sender, EventArgs e)
    {
       string script = " <script  type ='text/javascript' >";
       script = script + " try {";
       script = script + "obj = GetObject('', 'Outlook.Application');";
       script = script + "alert(obj.Session.CurrentUser.Address);";
       script = script + "}";
       script = script + "catch (e) {";
       script = script + "alert(e.Message + ' Not Supported');";
       script = script + "}; ";
       script = script + "</script>";
       ClientScript .RegisterClientScriptBlock(this.GetType(),"key", script);
    }



In this code, I have used JavaScript code. Using GetObject() method I am creating object for Outlook Application that will target to client machine Outlook object.

obj = GetObject('', 'Outlook.Application');

Now to get the Client Session, I am using Session property of this object (obj), using this Session property we can deal with all options of Outlook for client machine.
Like I have used, to get user name, I have used this code-

obj.Session.CurrentUser.Address

Here Address will point to Outlook user name of Client machine, for different client machine this line of code will give different user name based on outlook user name.

Output will be like this-



Outlook Username



Note- This example will work only with Internet Explorer.


Thanks
     

Monday, October 22, 2012

How to wrap Label text in asp.net?


If you don't wrap Label text in page, then it will disturb display settings of page controls, because Label displays text in single line.

To solve your problem just wrap the Label, using word-break CSS.

For this follow these steps-

Step1.  On top of the page, define this CSS in style section.

<style type="text/css">
        .clsWrap
        {
            word-break: break-all;
        }
</style>


Step2.  Now set this CSS to Label control using CssClass Property.

<asp:Label ID="lblMessage" runat ="server" CssClass="clsWrap"  width="100px"></asp:Label>


Now done, after crossing 100 PX length of Label text will be in next line. Same code you can use for GridView Label control also, means if you are putting Label control inside GridView Template then use same solution.



Thanks



Monday, October 15, 2012

How to add custom action to Windows Installer using C# ?


Visual Studio Package and Development wizard provides facility to create setup of your .Net Application. But in some cases we want to control the installation steps using custom action, means we want to add some custom functionality while installing the application.

Using custom action we can handle functionalities like-

-    Setting registry value while installation
-    Reading registry value while installation
-    Serial key implementation

Etc.

In this article, I am explaining - How to add custom action to Windows Installer in C#?

Here I am giving complete steps with pictorial representation. Follow these steps -

Step1: First, develop your application with required functionality.

Step2: For performing your custom action while installing application, we need to add "Installer Class" in application.  Add one "Installer Class" using "Add New Item" to the project like this-



Installer


Step 3: After adding Installer Class to the project , add your custom functionality , Here I am adding some code in "MyInstaller.cs" file-


//Code to perform at the time of installing application
public override void Install(System.Collections.IDictionary stateSaver)
        {
            System.Windows.Forms.MessageBox.Show("Installing Application...");
        }

//Code to perform at the time of uninstalling application
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            System.Windows.Forms.MessageBox.Show("Uninstalling Application...");
        }



Here I have added to events, this is for custom functionality, you can write your own code which you want to perform at the time of Installing and uninstalling application.

Step 4: Now set your application in "Release Mode" and build it.

Step 5: Now we have to add setup project for developed application, so right click on "Solution Explorer" and click on "Add -> New Project option".

Here you will get Project Templates dialog, Select "Visual Studio Installer" from "Other Project Types" option.

See this Image-


Setup


Step 6: After adding Setup project to solution, set project output for Setup project. For this right click on setup project and click on "Add -> Project Output"

you will get "Add project Output Group Dialog”, Here you need to set some setting like this-



Add Project Group


After setting click on "OK" button.

Step 7: Now to add custom action, again right click on "Setup project" and click on "View -> Custom Actions".


Custom Action



Now to set Custom action for installation right  click on "Install" click on "Add Custom Action",

Here double click on "Application folder" and select Primary output from Myproject and click on "OK" button.


Primary Output



Step 8: Repeat same for UN-installation process also.

Step 9: Finally your solution structure will be like this-


Solution Explorer

 

Step 10: Now build setup project and finally build the solution. Now you are done.

To test the complete process just try to install your application now, while installing application you will get message like this-



Installing Application



This is the same message which we have given in Install () event. Means whatever the action or code we will write in this event that will be perform at the time of installing application.

//Code to perform at the time of installing applicationpublic override void Install(System.Collections.IDictionary stateSaver)
        {
            System.Windows.Forms.MessageBox.Show("Installing Application...");
        }

And when you will uninstall application you will get message like this-


Uninstalling Application

 Here also we can see that the code which we have written in Uninstall () event that is executing.
Like Install () event we can handle Uninstall () event also.


//Code to perform at the time of uninstalling application
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            System.Windows.Forms.MessageBox.Show("Uninstalling Application...");
        }



These complete helps you to handle custom action while installation or uninstallation, Using these step we can implement serial key for application, means while installing application we can set expiration time for application and while uninstallation we can delete application related registry settings.



Thanks



Sunday, October 14, 2012

How to maintain selected JQuery tab during post-back?


JQuery -

JQuery is java-script file which provides built-in client side functionality. By including JQuery plug-in to page we can use these built-in functionality. It is just like a Header files in C, means to use JQuery functionality it is must that first we need to include JQuery plug-in to the page.

In this article, I am explaining - How to maintain selected JQuery  tab during post-back?

While using JQuery tab if we are making any post-back then JQuery tab doesn't maintain selected tab. Her I am giving code to maintain JQuery selected tab in post-back.

For this, I have used HiddenField variable to maintain the value of selected tab index. To explain this, I have designed .aspx page like this-

 <head runat="server">
    <link href="Css/JQuery -ui-1.8.20.custom.css" rel="stylesheet" type="text/css" />
    <title>hi</title>
    <script src="Scripts/JQuery -1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/JQuery -ui-1.8.20.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">

         $(document).ready(function () {
            $('#tabProfile').tabs({ selected: $("#<%= hdnTabIndex.ClientID %>").val() });
        });
      
       function funHoldValue(index) {
            $("#<%= hdnTabIndex.ClientID %>").val(index);
        }
    </script>
</head>

<body>
    <form id="form1" runat="server">
    <asp:HiddenField ID="hdnTabIndex" runat="server" Value="0" />
    <div id="tabProfile" style="width: 50%;">
        <ul>
            <li><a href="#tabsGeneralProfile">General profile</a></li>
            <li><a href="#tabsEduProfile">Educational profile</a></li>
            <li><a href="#tabsProfProfile">Professional profile</a></li>
        </ul>
        <div id="tabsGeneralProfile" style="text-align: center;">
            General Profile
            <br />
            <asp:Button ID="btnGeneral" runat="server" Text="OK" OnClientClick="funHoldValue(0);" OnClick="btnGeneral_Click" />
         </div>
        <div id="tabsEduProfile" style="text-align: center;">
            Educational Profile
            <br />
            <asp:Button ID="btnEducation" runat="server" Text="OK" OnClientClick="funHoldValue(1);" OnClick="btnEducation_Click" />
        </div>
        <div id="tabsProfProfile" style="text-align: center;">
            Professional Profile
            <br />
            <asp:Button ID="btnProfession" runat="server" Text="OK" OnClientClick="funHoldValue(2);" OnClick="btnProfession_Click" />
        </div>
    </div>
    </form>
</body>
 

Here we can see that, I have taken one HiddenField variable by named- hdnTabIndex.
In this code I my holding value of selected tab using this variable. I have declared one client side function by named- funHoldValue(). 

Using funHoldValue () function, I am setting selected tab value to  HiddenField variable. In this example I have designed 3 tabs, and for holding selected tab value I am using code like this-

  <asp:Button ID="btnGeneral" runat="server" Text="OK" OnClientClick="funHoldValue(0);" OnClick="btnGeneral_Click" />

Here I am passing the index for setting to HiddenField, like this-

 OnClientClick="funHoldValue(0);" 
 OnClientClick="funHoldValue(1);"   
 OnClientClick="funHoldValue(2);"   

 After setting value to HiddenField we have to set selected tab while post-back for that I have used this code-

$(document).ready(function () {
            $('#tabProfile').tabs({ selected: $("#<%= hdnTabIndex.ClientID %>").val() });
        });

This code will get value from HiddenField and set that value or index as selected tab index in JQuery tab. Here output will be like this-


JQuery Tabs




Thanks


Tuesday, October 9, 2012

How to refresh page automatically? - 4 different ways


Some of the sites those work on the basis of updated data like - Cricket score updates, Share market data, For those kind of sites it is required that to update or refresh the page automatically. In this article, I am  explaining - How to refresh page automatically?

Here I am showing 4 different ways to update or refresh the page automatically. This can be done using following ways-

1. Client side code
2. Server side code

Client side code-

In client side we have following options for this-

A. Using meta tag-

By setting the attributes value in meta tag you can auto refresh the page.

Like this-

<head runat="server">
 <meta  http-equiv="REFRESH" content="1">
</head>
 

B. Using reload() -

reload() function refresh the page explicitly, So use this function inside  setTimeout() function and give interval for setTimeout().

Like this-

<script type="text/javascript">
        function RefreshPage(time) {
            setTimeout("location.reload(true);", time);
        }
</script>


Now call this function on body load() event.

<body onload="RefreshPage(1000);">


Here 1000 is time to refresh the page after particular interval, you can change this time.

Server side code-

In server side we have following options for this-

A. Using Response.AddHeader()

you can set value for refreshing the page in Page_Load() event.

Like this-

protected void Page_Load(object sender, EventArgs e)
{
   Response.AddHeader("Refresh", "1000");
}


B. Using Timer control.

By setting the Interval and Enabled property you can refresh the page.

Like this-

first set Enabled property of Timer control= true
now set Interval property of Timer control= 1000 (your desired value)

last  you need to implement Tick() event for Timer Control-

protected void TimerControl1_Tick(object sender, EventArgs e)
{
    //Your code
}



Now done, these are some possible ways for auto refreshing the page; you can use any of these.



Thanks


Monday, October 8, 2012

How to compare DataTables?


Data Tables can be easily compared. This comparison can be done using Merge () and GetChanges() methods of DataTable class object.

In this article, I am taking 3 DataTable class object- dataTable1, dataTable2 and dataTable3
Here dataTable3 will hold the comparison result of dataTable1 and dataTable2.

Here is simple code-

//Merging datatable2 to data table 
dataTable1.Merge(dataTable2);

//Getting changes in dataTable3

 DataTable dataTable3= dataTable2.GetChanges();


In above lines of code, first I am merging datatTable1 and dataTable2 and combined result will be stored in dataTable1. Finally to get changes I am using GetChanges() method of dataTable1 object.

DataTable dataTable3= dataTable2.GetChanges();

Here GetChanges() method will return one DataTable class object and  returned result will be stored in dataTable3 object.



Thanks

How to pass more than one values from one page to another page using javasctipt?


There is lot of methods for passing data from one page to another page, but here I am explaining, 
How to pass more than one values from one page to another page using java-script?

I have created one example for showing this process, here is code-
Source page code (SourcePage.aspx)-

This is client side Function-
        
 <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        function TransferData() {
            var value= new Object();
            value.item1 = $('input[id$=txtItem1]').val();
            value.item2 = $('input[id$=txtItem2]').val();
            showModalDialog('SecondPage.aspx', value);
            return false;
        }
    </script>


In above JavaScript code, I have created one function called- TransferData(). In this function, first I have created one var type as object using this line-

var value= new Object();

Now, using JQuery I am getting control's values and assigning to "value" variable, here "item1" and "item2" are dynamic properties for value variable.

       value.item1 = $('input[id$=txtItem1]').val();
       value.item2 = $('input[id$=txtItem2]').val();


After that using showDialog() , I am passing value variable to next page.

   showModalDialog('SecondPage.aspx', value);

All these value can be accessed in next page using same properties.

Body Part-

  <div>
        <table align="center">
            <tr>
                <td>
                    Value1
                </td>
                <td>
                    <asp:TextBox ID="txtItem1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Value2
                </td>
                <td>
                    <asp:TextBox ID="txtItem2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="btnSend" runat="server" Text="Pass Data" OnClientClick="return TransferData();" />
                </td>
            </tr>
        </table>
    </div>



Now to pass data of this page to another page, I have called TransferData() function in onClientClick() event of Button.

Now SecondPage code (SecondPage.aspx)-

This is client side Function- 

 <script type="text/javascript">
        function FetchValues() {
            var value= window.dialogArguments;
            if (value!= null) {
                var item1 = value.item1 ;
                var item2 = value.item2;
                alert('item1 =' + item1 + ', item2 =' + item2 );
            }
            return false;
        }
    </script>


To get passes data from source page, I have written one client side function FetchValues(). In this function, I am getting passed data variable using this code-

var value= window.dialogArguments;

Now to get values, first I am checking for null and then accessing value like this-

   if (value!= null) {
                var item1 = value.item1 ;
                var item2 = value.item2;
                alert('item1 =' + item1 + ', item2 =' + item2 );
            }


Body Part-

<body onload="FetchValues();">
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>


I have called FetchValues() function in onload() event of body, based on our need this function can be called anywhere.

Note-
In this code I have used JQuery, so don't forget to include JQuery plug-in in your page.


Thanks