February 2008 Entries

If you've existing XML files that you need to manipulate in C# there is no need to write your own class by hand. Visual Studio ships with a little known tool XSD.exe which will do all the leg work for you.

Step 1 (from a command prompt):

This will create an XSD based on the XML file, which in step 2 will allow the tool to create the C# class(s)

XSD.exe [XML_FILE]

Step 2 (from a command prompt):

Call the tool again, this time providing the newly created .xsd file, along with /c (for classes, you could use /d for a dataset) and /n for your project's namespace

XSD.exe [XSD_FILE] /c /n:[PROJECT NAMESPACE]

Other parameters that might be useful are /o: (out) which specifies the output directory

Step 3:

Add the newly created .cs file to your project, and bingo, job done!


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

Following on from my last post I've since discovered how to access the User's Profile in an external Class Library (like I've mentioned before I prefer to factor out all business logic code into a separate class library).

Anyway the code is almost the same, although unfortunately you don't get strongly typed properties out of the box (unless you create your own profile object, inheriting from System.Web.Profile.ProfileBase).

Here's how to access the current user's properties (using FirstName/LastName) as before, only this time in a separate dll...

string firstname = HttpContext.Current.Profile["FirstName"];
string lastName = HttpContext.Current.Profile["LastName"];

And as before it's also possible to access other user's profiles (where 'Brad' is the user's username):

ProfileBase profileBase = ProfileBase.Create("Brad", true);
string firstname = profileBase["FirstName"];
string lastname = profileBase["LastName"];

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

One of the many improvements in ASP.NET 2.0 was the Provider based architecture, and the most under used (certainly by me) is the User Profile Provider (click here for more info). You simply define the properties you want your users to have in the web.config (see below)

<profile enabled="true" defaultProvider="MyProvider">
     <providers>
       <add name="MyProvider" connectionStringName="MyConnection"
            applicationName="/" 
            type="System.Web.Profile.SqlProfileProvider" />
     </providers>
     <properties>
       <add name="FirstName" type="string" />
       <add name="LastName" type="string" />
     </properties>
   </profile>

And .NET takes care of the rest... you can then access the profile programmatically as follows:

string firstname = Profile.FirstName;
string lastname = Profile.LastName;

Simple!

There are a couple of problems using the SqlProfileProvider, firstly the data is serialised and stored in one field in the database - making it trickier to run SQL queries on it... and the second (or so I thought until recently) is accessing other user's profile data - for example an administrator should be able to see the other user's FirstName and LastName values.

However the second issue isn't a problem at all, it is in fact possible to view/edit another user's profile with the following code:

ProfileCommon userProfile = (ProfileCommon)ProfileCommon.Create("Brad", true);
userProfile.FirstName = "Paul";
userProfile.LastName = "Bradley";
userProfile.Save();

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist

If like me you've more than one computer at home and would like to be able to RDP to them directly then read on! If you follow the steps below you will be able to add extra "listening" ports to each of your computers so you can access them from anywhere... The reason you need to do this is to allow you to specify specific port forwarding rules in your firewall/router. The default RDP port is 3389 so I leave my main computer alone, but if i want to access my media centre I can get to it by adding :3390 to my hostname/ip. Any other computers you can just increase the port number.

Step one:

Run regedit from a command line on the computer

Step two:

Browse to the following key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\
Control\Terminal Server\WinStations\RDP-Tcp  

Step three:

Export the key to your desktop - it will create a .reg file.

Step four:

Open the newly exported registry file in Notepad.

Step five:

The second line of the file should contain the path of the exported registry key - its important to change this, otherwise when you re-import it the existing RDP settings will be overriden. I prefer to create additional rules so you can always still access the computer normally inside your network. So the second line should end with:

WinStations\RDP-Tcp]

I suggest changing it to:

WinStations\RDP-Tcp-3390]

This will then create a brand new key when we re-import it later

Step six:

Still in notepad search for "PortNumber", it will look like this:

"PortNumber"=dword:00000d3d

You need to change its value - but its in hex so you'll need to use an online tool - or calculator in scientific mode. The following sets the port number to 3390:

"PortNumber"=dword:00000d3e

Step seven:

Save the file (make sure it's saving as a .reg)

Step eight:

Double click the saved file which will re-import it. (accept the confirmation messages)

Step nine:

Run regedit again and browse to your new key - just to check its there ok.

Step ten:

Re-boot the machine and try RDPing with :3390 on the end of its IP address.

Step eleven:

Login to your router/firewall add an incoming rule for port TCP 3390 to the IP of the relevant computer.

 

Repeat the above steps for each of your home computers (making sure to change the new port number each time!)


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist