Friday, August 28, 2009

Getting user role ID's

Ok, so I ran into this issue when I was working on a website using's Membership class. I wanted to get the user role ID's instead of just their string names. That way if a string name changed I wouldn't have to go and update other tables.

Anyway, I couldn't find anything on a method that does that for you but I did find an article on this site , which tells you how to get the user ID. From there you can just query the aspnet_UsersInRole table in your membership db and it will return all the role id's for that user.

Tuesday, August 18, 2009

Subsonic 3 Setup

I as a new programmer had a rather difficult time setting up Subsonic in VS Pro 2008 and there-after learning the syntax. So in this post I am going to post some instructions that might be of help to me and others in the future.

First, make sure you have already created a connection string in your web.config or app.config file.

1. First you download their Zip file here.
2. Extract the files and look for the directory T4 Templates >> Active Record.
3. Inside of the Active Record directory find the file "Settings.ttinclude".
4. Open it with a text editor and change line 21 to:
const string Namespace = "your desired namespace";
5 Set the ConnectionStringName equal to your connection string's id.
6. Set the DatabaseName constant to your database's name.
7. Save Settings.ttinclude.
8. Right click on your project and add a reference to SubSonic.Core.dll. It is found under the subsonic Bianaries folder.
9. Now this is the tricky part...I saw in their instructions that I needed to copy their files into my project and then in visual studio you are supposed to right click on their template files and then click on "Run custom tool". Unfortunately I couldn't see that option when I right clicked on the files. That is because you need to place them in a class library first. So you need to right click on your solution and add another project. Make it a class library project. Then go back and add a reference to it in your main website project.
10. Now copy all of the files from subsonic's T4 Templates folder into your new class library project. You also need to create an app.config file and add the connection string again to that file. You also need to add a reference to SubSonic.Core.dll.
11. Now right click on each one of the files and click "Run custom tool".
12. Now build your class project and build the solution, you may have to clean it before it will work properly.
13. Now if you add your namespace reference you should see a bunch of new variables. It looks like Subsonic 3 generates two classes for each table. One will be named the same as your table and the other one will be your table name with "Table" on the end.

Each time you make a change to your database you will need to run the custom tool for each file and then rebuild.

A simple select statement:

MyDatabase db = new MyDatabase();
SqlQuery simpleQuery = <actualtablename>().Where(tablenameTable.idColumn).IsEqualTo(1);
List<actualtablename> blah = simpleQuery.ExecuteTypedList <actualtablename>()

You can also use linq chained methods like this: person.find(s=>s.person_id = 10);
To create a new person row you just enter:
person p = new person();
p.person_name = "bleh";;

Sunday, August 9, 2009

Thursday, August 6, 2009

VS2008 Connection issue with SQL Server 2005

So I was getting a strange error: "Could not load file or assembly 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=, Culture = neutral, PublickKeyToken=89845dcd8080cc91 or one of its dependencies. The system cannot find the file specified.

Luckily I found a fix on this site:

He says to install the following files which can be downloaded at:

Microsoft SQL Server System CLR Types
Microsoft SQL Server 2008 Management Objects
Microsoft SQL Server 2008 Native Client