Thursday, February 19, 2009

Populate the country list without usign the DB - using culture info

No need to store the country list in DB to fetch it and show it in the application.

Use these steps to do so:-

1. It uses the System.Globalization Namespace to get all the cultures that are not only native on the system, but also all the ones installed with the .Net Framework. Add
using System.Collections.Generic;
using System.Globalization;
and then you can write the code like this to get all the countries into a list





2. The list comes as an unsorted one. We can implement a list sort function to sort this. But in my opinion the fastest method is to sort the control that is used to bind the list - in most cases it will be a dropdownlist obviously. So here is the fucntion to sort the dropdownlist.
Add
using System.Collections;
And now the below function to sort the dropdownlist













3. Now we are done. Use the functions like this

ddlCountry.Items.Add(country);
SortDDL(ref this.ddlCountry);

Thursday, February 5, 2009

User.Identity.Name returns Empty String

@ times, in a web page if we try to access User.Identity.Name an empty string is returned...

Wondering the cause for it??

Check your Web.config.
This problem occurs because the authentication-related variables in the ServerVariables collection are not populated if you use Anonymous Access security to access the .aspx page. This problem can also occur if you give the Anonymous user access in the section of the Web.config file. To avoid it, proceed with the following two steps.
1. Change the authentication type to anything other than None.
2.This is the important step, deny the access for anonymous user




Sending emails using System.Net.Mail

While using
SmtpClient sm = new SmtpClient("localhost")
for sending mails, the system shows error
'Cannot send mail'.

However, this problem comes only in .net 2.0 One reason for this is the McAfee antivirus!!! To solve the problem. Go to the Virusscan Console -> Select Properties and in the 'Port Blocking' tab, uncheck the 'Prevent mass mailing worms from sending email' option.

If after this, you get a message, cannot relay to 'address', then enable Relaying for your SMTP server.

dll does not represent a strongly named assembly

Problem: When we try to use the Strong Name Tool (sn.exe) to sign assemblies with strong names, we get the error "dll does not represent a strongly named assembly"

Solution: You need to specify a strong name key file before you can use the command prompt "sn.exe". For this, go to Project Properties -> Signing and tick the 'Sign the assembly'. From the drop down select the '...' option and create a new file. Once you this, build your project again and now you will be able to use the 'sn.exe' without any problem.

My First WCF Experiment - In VS 2005

One Fine day, one friend of mine asked --' Hey so what do you know about WCF'..... Ehh I replied Yeah Why Not , I certainly know.... "World Computer Foundation"... And he quietly went away saying anything.

Funny thing apart, I felt somewhat shameful in this and a 2 year lag from other techies and finally determined to research what is this stuff.

Made a search in the web to found out What is WCF ? How this works? How to run a sample demo?

I am going to share my first WCF experience of creating the first WCF service with you.... So that someone else can benefit from this......

I used my system with Visual Studio 2005 enterprise edition installed for this. Please see below the step by step process I did:---

1. Started ... OOps where is my .NET 3.0. Lesson1 -> You need Microsoft.NET 3.0 or higher to work with WCF. You can download that easily from
http://www.microsoft.com/downloads/details.aspx?familyid=10cc340b-f857-4a14-83f5-25634c3bf043&displaylang=en

2. We can start building the WCF service first. Let us call it by the name MyFirstWCFService.

3. Start VS 2005 and Create new ASP.NET Webservice.. hope u all know this . If you have installed WCF extensions for VS 2005, then You will get a Create new WCF service template inbuilt. But I am creating a new ASP.NET webservice for creating the WCF service.

For installing WCF extensions for VS 2005, I went to Microsoft site, but it was not there- removed and now it comes with .NET 3.5- Outdated hmmm...
At last I found that from an extensive web search and here you go...
Please click here http://rapidshare.com/files/194386807/vsextwfx.msi

4. Now you have created a new ASp.NET web service, first we can remove the service.svc file-- We don't need . It is for way old ASP.NET web service and now we are modern and d0n't need the old damn thing hehe...

5. Add reference to the God, the heart , the soul and all-in-all of a WCF service ---
System.ServiceModel.dll.
If you are using a CTP or RC1 you will probably need to browse to this file. If not it will be listed on the .NET tab. Otherwise browse to the
Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation directory
to find this cutie...

6. Now you have added the dll file, Select the Service.cs underneath the App_Code folder. Wrote the following code.....



7. Save the service.cs file now and now we can add our next file needed Service.svc file . For this there will not be a entry in add new item in project context menu. For this selet a text file form context menu and name it to Service.svc. Now add the following code to this


8. Having saved this we can create our last file needed web.config file. Add a new configuration file from the add new item menu from project menu. and add the following removing the default entries to web config that VS 2005 makes .





9. Now the coding part is over and now we can move on to the deployment part. For this open IIS Admin page - InteMgr.exe and add a new virtual directory named MyFirstWCFService and assign a new folder to hold this virtual directory.


10. Now right click on the VS 2005 ASP.NET webservice project that we created and then click Publish Website giving the virtual directory as the one we set in the step 9. Please select Local IIS tab in the left to get all the virtual directory options in the Publish New Website Wizard.

11. Hurrah and we have finished and deployed our first WCF service and we are going to run it. Open any browser and type


http://localhost/MyFirstWCFService/Service.svc

Ohh NOOO!!! IT just displays the code that is in the Service.svc file. Damn... Started the research in google and at last found the solution to this. Download the following file


This is a magic file named CleanIISScriptMaps.exe. run this in command prompt and it just does the trick for you!!!!!.

Now having run this run the url again and yes ... My first WCF service have started working showing me the WSDL associated with the service. I am top of the world!!!!!!!!!!

12. Now I need to test the service I have created. For this create a sample windows application or console application . Add the soul and heart - yes System.ServiceModel.dll to that project. Then add "Service Reference"== this is a new term ... you will get this menu item when you install the WCF extension for VS 2005 as specified in step 3. In the service reference add the service URL as


http://localhost/MyFirstWCFService/Service.svc?WSDL

Give the name as "WCFServiceDemo". Now click Ok to create the service reference.

13. Step 12 will run the SVCUtil in the background for creating the client and we can use it to call the service.

14. Now in any event - if sample windows application create a button and label and then add the following code to the button click event.

WCFServiceDemo.TestWCFService client = new
WCFServiceDemo.TestWCFService ();
lbl.Text = client.MyFirstFunction(“Your Name”);

Intellisense will help you write this code.:-).

Now click the button and yes you can see your first virgin WCF result in the client.......

Hello: "Your Name"

This completes the task of creating the first WCF project. For specific terms and other doubts please read the WCF articles in MSDN or read some good Books..............

Will be back soon with my first WPF and Silverlight experience ...... Wait for that guys!!!!