Showing posts from April, 2010

How to get client "IP Address" using /C#

I have found that in various forum newbie are very frequently asked this type of question how to get client IP or get mac address or country. So that I would like to share this for that user. Though its very simple but helpful.

To get the client IP you can use the following function
    string sClientIp=Request.UserHostAddress();
    string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

Tip:2:To avoid Proxy IP:

To get the IP address of the machine and not the proxy use the following code
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; This will returns the client ip in string format.

    string ip;

REMOTE_ADDR does not always provide the users IP but rather the ISPs' IP address so fi…

Validation control not work in FireFox or other browser

When I use validation control on it works for IE fine but failed to work in Firefox. After goggling I will find the solution. I share this code here, hope it may help others.

The ASP.Net validations controls only work client side in IE.
You can make them work in other browsers, coding as below:



if ( !Page.IsValid )
//Continue Your code


If Not Page.IsValid Then
End If
'Continue Your code

Have a nice day.

how to get MAC address using C#

How to get the MAC address of  system using

Now I want to share with you some simple but important for our rapid development.
Here I want to discuss how to get system MAC address using c# ,

First way:
You need to import the System.Net namespace for this to work.
This will support IPv4 and IPv6.

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
        IPInterfaceProperties properties = adapter.GetIPProperties();

Second way: public string GetMACAddress()
  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  ManagementObjectCollection moc = mc.GetInstances();
  string MACAddress = String.Empty;
 foreach (ManagementObject mo in moc)
     if (MACAddress == String.Empty) // only return MAC Address from first card   
              if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAdd…

How to detect browser Information in

Sometimes some fresher asked me how they get browser information when using In online you can find lots of resources about. I have placed this code snippet form msdn which may help newbie.

Place this code under any button event or Page_Load event:
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = "…

How to bind Data in Customized Gridview

In gridview control is widely used control. It has lots of properties and attributes which help the developers for rapid development. Generally we can use Gridvew to show data in tabular format.
But It has a lots of events and features which is make this control a most powerful control for
There are various types of field in Gridview such as:
These fields gives developer more flexibility to customize the gridview. Paging is efficiently handling by Gridview it is one of cool feature of it. There are various way to bind data into gridview.

Today I want to focus how to bind data to a customized gridview. I have used this following way to bind customized gridview very frequently. Obviously,there may be more easier way to bind in customize gridview.
Here I have used BoundField,Templatefield to customize the grid.

Here is the aspx code for Gridview:

<asp:GridView ID=&q…

Unable to start debugging on the web server. Could not start ASP.NET or ATL Server debugging.....

Yesterday I had started my work at new work station. Very recently I needed to test a web application on localhost. I found that IIS is not installed so that I installed it myself. Then I hosted it in localhost and try run but showed me an error. Today I want to share with you how to solve this problem.

After developing an ASP.NET Web application by using localhost as the Web server, and config the Web site settings in Microsoft Internet Information Services (IIS) to use the localhost or specific IP address for Web Site Identification. Then, when I try to run the application, then receive the following error message:

Error while trying to run project: Unable to start debugging on the web server. Could not start ASP.NET or ATL Server debugging.
Verify that ASP.NET or ATL Server is correctly installed on the server.

After search in google I have found lots of solution. But suddenly remembered that I have insatalled IIS after .net framework installed. I hope that this …

Single Sign on (SSO) Using Cookie in

There are various ways to use Single Sign on(SSO) in web application. We can use cookies, session (state server), SAML and web services etc. Now we would like to give a brief overview of how to use cookie to implement Single Sign on(SSO) in web application.

Assume that we have two web application hosted on different virtual directory but under same domain. As for example, our root domain is: and
Other two virtual directory hosted under this domain are

If we login successfully in cookiesite1 then it writes the login information in cookie and now opens another tab or a new window in same browser (IE, FF whatever you like). Place this address in address bar logged in automatically in cookiesite2. When we try to access in cookiesite2 –> Default.aspx it checks the login information from cookie. If d…