C# Programming for Beginners | Visual C# Training Courses

After having written a three page tutorial on some basic C# programming concepts, (if you haven’t read it, the first part is here: C# Classes, C# Methods and C# Objects) I’ve realised that if this website is entitled, C# Programming for Absolute Beginners, I’m going to have to go back to the absolute basics.

So, in line with this I’ve decided to write a series of C# Training Courses on the absolute basics of C# programming.  These tutorials will aim to teach basic programming to you, even if you’ve never read code before. 

I will use plenty of C# examples and sample C# source code – this should help you become familiar with how the code is used, and help you to recognise the basic building blocks of programs.  The tutorials will help you to understand how to use C# code, and accomplish this all in bite [byte?] ;-) sized chunks.

These mini C# training courses will cover many different aspects of C# development some of which are:

(These are all I can think of now - I know there is a lot more and I will cover plenty more as I go)

Beginners Guide to C# Programming Syntax: 
What are those curly brackets there for?  What is public, private?  Where do I put what?  How do I write code that actually does something?

When I started out these were the sort of questions that I had and didn’t want to ask because I though I would sound (and look) stupid.

A Basic Windows Programming Tutorial: 
How to create forms.  Naming conventions.  Getting and using user input.

Web Development - What is ASP.NET? 
Web development in C#.  Differences to Windows Programming.

Database Programming - What is SQL?
What are databases?  Why do we need to use them?  Database access using C#.

Along the way I’ll probably cover more intermediate subjects and one or two advanced concepts.  I’ll try and remember to indicate whether or not I consider the programming to be Beginner, Intermediate or Advanced, but the majority will be aimed at beginners.

If I don’t get through all these subjects in the same order please don’t shoot me – this is really just a rough guide for me as much as you.
Somewhere along the line, once I have enough content up, I’ll add a contents page for easier navigation through the mini courses / tutorials.

For now, if you feel you want to follow these tutorials, please note that I use a few different tools when programming.  These are the ones I generally use and will refer a lot to during the tutorials.  You can download them all from their respective websites but initially you will only need Visual C# 2008 Express Edition.

If you already have these then you are all set!

Visual C# 2008 Express Edition

C# Programming for Beginners | Integrating SQLMembershipProvider with Existing Database

What is the SQLMembership Provider?

Basically the SQL Membership Provider provides membership and roles for your websites automatically. It does this by installing a database that can enable you to help users to register and login easily, allowing you to spend more time on building your website and adding content.  This is instead of trying to implement a custom designed membership database, which is time consuming.

You need to run the aspnet_regsql tool in order to install the membership and database.  This tool can be found at the path C:\ >> Windows >> Microsoft.NET >> Framework >> v2.0 >> aspnet_regsql.  This is a command line tool that can be run from the command prompt as well.  However if you double-click it, it will run a wizard - this is what I used.

I ran the aspnet_regsql tool first up in order to use the SQLMembershipProvider.  This gives you the opportunity to add the provider using an existing database or to create a new (default) database.  If you use the default database the tool will create a database called ASPDBNET.mdf.  I had an existing database already and so used Windows Authentication and the server name as: localhost\SqlExpress, then selected my database from the dropdown window.  Following all the prompts I finished the wizard.

Now, if you open up Sql Server Express (I use SQL Management Studio Express) and navigate to your database you will see that the tool has added a whole lot of database tables all starting with aspnet_.  These are the tables that are needed for the SQL Membership Provider.  So far so good.

One more thing to remember to do is to set your authentication mechanism to forms authentication.  Look for the section that is called <system.web>.  Directly below that add the following line: (or change it if it is set to “Windows”)


 <authentication mode="Forms" />

Now we need to add a new user in order to test the login controls.
Open the ASP.NET Configuration for your site.  The icon for this is a small red hammer next to a blue/green earth on the top of the Solution Explorer.  

Once this opens click on the security tab and select “Create User”.  Fill in the textboxes – be sure to write down the passwords and usernames.

Now go back into Visual Studio and add the login control and the CreateUserWizard to your application, add a loginname control as well.  All these controls are found in the Data section of the Toolbox in Visual Studio.  Click the green arrow to run the ASP.NET application.  Then test the login control.  If you login correctly the loginname control should display your login name.  It should work fine.

When I attempted to use the SQLMembershipProvider in order to use the built in login control and CreateUserWizard –– I found that the controls worked well, I could log in and create new users with no hassle.  However, when I went into Sql Management Studio and ran a query on the table in my database, I could not find any of the records of the new users (The query was very basic:  SELECT * FROM aspnet_User).  What gives?

Well after a lot of fiddling and testing I found that the entries that are made in the web.config file for the provider set the default provider to the default database – ASPDBNET.mdf  When you run your application and register a new user, this then creates another database in your App_Data folder.  Even though you told the regsql tool to create the membership database in your existing database, it has created the default database when you registered a new user as well.  It then writes the new user details to the database in your App_Data folder and not your existing database in Sql Express.

So how do we sort out this problem?
Go into your web.config and find the <connectionStrings/> section.  Here you need to add the details of your connection string to your SQL database. 


        <add name="YourConnectionStringName" connectionString="Data Source=YourLocalHostName\SqlExpress;Initial Catalog=YourDataBaseName;Integrated Security=SSPI;"/>

You need to find the authentication section again and add a new section underneath called <membership>.  Here you change the default provider to your providers name (just give it any name).  Then you need to add another section under that called <providers>.  Here you name your provider (call it the same name as the name you gave to default provider above).  There are other properties that you can change here is an example:


        <add name="YourConnectionStringName" connectionString="Data Source=YourLocalHostName\SqlExpress;Initial Catalog=YourDataBaseName;Integrated Security=SSPI;"/>

      <authentication mode="Forms" />
      <membership defaultProvider="YourProviderName">
        <!—Add this section to use your database instead-->
          <clear />
            applicationName="YourApplicationName "
            passwordFormat="Hashed" />

Now we need to add a new user in order to test that we are using the correct database this time.
Open the ASP.NET Configuration for your site.  Once this opens click on the security tab and select “Create User”.  Fill in the textboxes – be sure to write down the passwords and usernames of your new user.

Back in Visual Studio test your controls again by running your site.  The logins should all work correctly.

Go back into SQL, make sure you select your database from the list and run your query again: SELECT * FROM aspnet_User.  This time you should get one line returned from your aspnet_User table containing the user name that you entered last.

If you do then you have correctly integrated and configured the SQLMembershipProvider with your existing Database!