.NET Tip – Getting logged in user name in ASP.NET web application

Frequently we desire to autopopulate some fields on our web form with the current logged in username and domain.


How to get that information?


Well, User.Identity.Name comes to the rescue.


Set the text property of the field to User.Identity.Name and it will show the logged-in username prefixed by the domain information

Want to work for Microsoft? Send me your resume

I contract with Microsoft through Volt (www.volt.com) and currently there are lot of contract positions available.


If you are interested to work with the best brains in the world and love developing challenging products used by millions in the world, send me your resume at vipul_d_patel@hotmail.com . These are strictly contract positions.


US Citizen, Green card or H1 visa required. My company Volt does not sponsor new H1, but will willingly transfer your existing H1.


Looking forward to receiving your resumes. Just send me your resume and I will try to find a position bets suited to your skills.


If you have any questions, feel free to send me an email  and I will reply to you as soon as possible.

Internet Explorer Beta2 now available

Check it out.

http://www.microsoft.com/windows/ie/ie7/default.mspx

Visual Studio Tip of the day – Refactoring – Extracting method

You notice that you have a chuck of code which could easily be transitioned to a new function. How tdo you do that?


Again, Visual Studio Refactoring menu comes to the rescue.


Suppose you have the following code in your function


public void Myfunc()


{


   Console.WriteLine(“a”);


   Console.WriteLine(“b”);


   Console.WriteLine(“c”);



   // Do some processing here.


   Console.WriteLine(“a”);


   Console.WriteLine(“b”);


   Console.WriteLine(“c”);


}


 


We realize that code containing Console.Writeline is replicated. Select one set of the Console.Writeline instructions and right click > Refactor > Extract Method…


Type the name of the new function you want to create containing the selected lines and Click OK.


A new method containing the selected lines is created. So your code will look like


public void Myfunc()


{


   NewMethod();



   // Do some processing here.


   Console.WriteLine(“a”);


   Console.WriteLine(“b”);


   Console.WriteLine(“c”);


}


private static void NewMethod()


{


   Console.WriteLine(“a”);


   Console.WriteLine(“b”);


   Console.WriteLine(“c”);


}


 


Keyboard shortcut: Ctrl R + Ctrl M


Cavaet: You will have to delete the second set manually as currently VS editor is not smart enough to replace all the occurances of the selected lines. Maybe in the next version we can get that feature.


 

Visual Studio Tip of the day – Refactoring – Changing variable names

Did you mistype a variable/function/property only to realize it in the code review and are frustrated over the time you will need to spend to correct it across the whole source code?


Visual Studio 2005 has a new feature called refactoring by which you can rename a property/function/variable at one location and the same will be replicated across all the location where the property/function/variable is referenced.


To do that, select the property/variable/function you desire to rename and right click and select Refactor > Rename. A Rename window will appear and you can select whether you want to preview the reference changes, or you want to change the entity in the comments also.


Keyboard shortcut: Ctrl R + Ctrl R


 

VB gets a LINQ equivalent

With the release of the LINQ CTP for Visual Basic, VB matches C# tooth and nail (purely from the LINQ perspective)


CTP version features Intellisense, Dlinq support, support for XML literals,


Download link: http://msdn.microsoft.com/vbasic/future and http://msdn.microsoft.com/netframework/future/linq/default.aspx

Is my webservice really a webservice

How to tell if your webservice is really a webservice which will interact seamlessly with external entities?


Well, keep the following in mind


1. Uses WSDL.  A Web Service should expose its service contract using WSDL.  If it can’t give you a WSDL document, it’s probably just XML over HTTP…


2. Uses SOAP.  All messages sent from and received by the Web Service must use SOAP formatting.  If it’s not using SOAP it’s probably just XML over HTTP…


3. Uses XSD.  All data types in the SOAP payload must be XSD compliant.  No platform native types are allowed.  If it’s not using XSD it’s probably just XML over HTTP…


4. Uses XML.  The underlying messages should of course be formatted using XML.


5. No Arbitrary Binary Data.  The message payload should 7 bit ASCII and should contain no embedded binary blobs.  Any binary data passed over a Web Service should be sent using either SwA, DIME or MTOM (preferably MTOM).


6. Transport is likely to be HTTP.  Although not a requirement, the majority of Web Services today use HTTP as the transport.  Compliant Web Services should definitely work over HTTP.


7. Discovery can be through UDDI.  Again although not a requirement, it should be possible to host the Web Service endpoint using UDDI.


8. Agreed Versions of Specifications.  The versions of the above specifications (WSDL, SOAP, XSD, XML, HTTP, UDDI) should be in line with the latest version of the WS-I Basic Profile (http://www.ws-i.org) – to ensure Web Service compliance between vendors.


9. Operations should be Document Style.  Operations to/from a Web Service should be Document/Message Style (e.g. SendOrder(order o)).  RPC style should be avoided (e.g. SetOrderLine1(orderId id)).


10. Should be compliant with WS-*.  Compliant Web Services should be able to accept WS-* payloads and extensions for Security, Reliability and Transactions (although not all stacks today support these yet).


Source: http://blogs.msdn.com/smguest/archive/2006/01/26/518020.aspx

How to: Determining programmatically if DLL is registered

Here is a C# code snippet to determine if a particular DLL is registered or not.


 


[DllImport(“kernel32”)]


public extern static int LoadLibrary(string lpLibFileName);


 


[DllImport(“kernel32”)]


public extern static bool FreeLibrary(int hLibModule);


 


public bool IsDllRegistered(string DllName)


{


      int libId = LoadLibrary(DllName);


      if (libId>0) FreeLibrary(libId);


      return (libId>0);


}


 


Source: http://blogs.msdn.com/asanto

Visual Studio Tip of the day – Format Document

Time and again we write code and our brackets get out of visual sync, i.e. they no longer appear as a coherent set even though they may be.


 


In Visual Studio, there is a feature known as Format Document which will align the code systematically.


 


It can be invoked by the key combination of Ctrl K + Ctrl D


 


Suppose you code looks like


 


namespace LogFileCheck


    {


    class Program


        {


        static void Main(string[] args)


        {


            TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);


            TextWriter writesr =


                new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”,


                false,              Encoding.UTF8);


            while (sr.Peek()


                != -1)


                            {


                string line = sr.ReadLine();


                if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))


                    writesr.WriteLine(line);}


 


                sr.Close();


            writesr.Close();


        }


    }


}


 


Press the magic keys Ctrl K + Ctrl D and voila, all your code looks pretty organized as under:


 


namespace LogFileCheck


{


    class Program


    {


        static void Main(string[] args)


        {


            TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);


            TextWriter writesr = new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”, false, Encoding.UTF8);


            while (sr.Peek() != -1)


            {


                string line = sr.ReadLine();


                if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))


                    writesr.WriteLine(line);


            }


 


            sr.Close();


            writesr.Close();


        }


    }


}


 


Want to format only a small selected section of the dirty code?  Select the area you want to format and press Ctrl K + Ctrl F.

Visual Studio 2005 – A Guided Tour

Want to learn more about Visual Studio 2005.


MSDN magazine folks have come  up with a new issue dedicated solely to the new IDE.


Check it out online at http://msdn.microsoft.com/msdnmag/issues/06/00/default.aspx


 

« Previous PageNext Page »