Showing posts with label SQLCE. Show all posts
Showing posts with label SQLCE. Show all posts

Thursday, March 28, 2013

Adding custom fields to a VS2012 MVC4 SimpleMembership system

Adding custom fields to a VS2012 MVC4 SimpleMembership system
Add custom columns/fields to your MVC4 SimpleMembership "UserProfile" table
MVC 4 in Visual Studio 2012 introduces the new SimpleMembership Provider as its default web-based authentication system. While this system is much simpler than the earlier ASPNETDB system that it replaces, there definitely are a few new and neat tricks to be learnt.

One of them is: How do I add custom fields to my MVC 4 registration system?

It can be done in just 4 simple steps:

1. Start up VS 2012, and create a new MVC 4 web project. Set up your SimpleMembership registration system - for some help, please see post on setting up a SimpleMembership Provider in a VS2012 MVC4 project.

2. Next, run the project and navigate to the login page, so that the tables get created. It is not necessary to create a user at this point.

3. Then, in your database, alter the table UserProfile, and add a column: FullName (nvarchar(50)).

4. Next, add/reference the new FullName field in the following programs in your project:
In class UserProfile, in AccountModels.cs (to ensure the class matches the table columns):

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        // Customization: Field(s) added
        public string FullName { get; set; }
    }

In class RegisterModel, in AccountModels.cs (to enable handling on the registration form):

    public class RegisterModel
    {
        // Existing code
        // ...

        // Customization: Field(s) added
        [Required]
        [Display(Name = "Your full name")]
        [DataType(DataType.Text)]
        public string FullName { get; set; }
    }

In the Register.cshtml view (so it'll appear on the user registration form), add a new li element with the following content:
    @Html.LabelFor(m => m.FullName)
    @Html.TextBoxFor(m => m.FullName)

In the [HttpPost] Register ActionResult, in AccountController.cs, modify the call to the CreateUserAndAccount method (to write the new column to your database):
    // Customization: Field(s) added when creating a new account
    WebSecurity.CreateUserAndAccount(
        model.UserName,
        model.Password,
        new { FullName = model.FullName }
        );

Now run your program and register a new user, supplying a value for the FullName field... success! The new row created in your database table contains the value supplied for the new column!

Note: This post covers how to create a custom column. A near-future post will explain how to access the value of that column from within your VS 2012 MVC 4 projects. Please re-visit my blog occasionally :)

Happy coding!

Monday, March 11, 2013

Deploying your site with SQL CE 4.0 to shared webhosting

If you have a VPS or dedicated server, you got full access to it and normally shouldn’t run into any problems. But when you’re using shared webhosting, your actions are very limited. This post describes the errors you’ll most likely run into the first time you try this, and how to solve them.
You can either deploy from within Visual Studio or FTP the files to your host. If you hit the URL to your website, you’ll most likely get following configuration error with the according web.config fragment below:
Parser Error Message: Could not load file or assembly 'System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
You get this error because most providers don’t have SQL CE 4.0 assemblies installed (yet?). This means you’ll have to 'bin deploy' the assemblies together with your site. Visual Studio 2010 provides a "Add Deployable Dependencies..." context menu item, which let you choose to add the required assemblies for Razor or SQL Server Compact to the Bin folder.


With some 'luck' you hit the next error once you solved the problem above and refreshed the page:
Parser Error Message: Access to the database file is not allowed. [ 1884,File name = somepath\App_Data\YourDatabase.sdf,SeCreateFile ]
For this error you have to give read + write access to the App_Data folder, usually done in the File Manager of your hosting control panel. If you got this error on your private server, check if the service running the Application Pool (e.g. Network Service) has these rights on the folder. You will also have to add following line of code (e.g. in the global.asax file):
AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
This is because, by default, SQL CE blocks access when the connection is opened under the ASP.NET networking process. Once I ran through these steps, I got everything up and running without any hiccups.

Source: http://www.bartlannoeye.be/blog/deploying-your-site-with-sql-ce-4.0-to-shared-webhosting-part-2