Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Some spammers deserve a bit of respectRunning IE6, IE7 and IE8 on win7 »
    comments

    If you download ASP.NET MVC from Microsoft, install it and create a MVC website you might run into a couple of gotchas in regards to the database. First of all ASP.NET MVC use SQL Server Express by default in the connection string. So if you were to run your ASP.NET MVC website, then clicking on login you would get this error:

    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

    A little more info below that error will tell you that it is trying to create a SQL Server Express database. Here is that info

    SQLExpress database file auto-creation error:

    The connection string specifies a local Sql Server Express instance using a database location within the applications App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

    If the applications App_Data directory does not already exist, the web server account must have read and write access to the applications directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.

    If the applications App_Data directory already exists, the web server account only requires read and write access to the applications App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the applications App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server accounts credentials are used when creating the new database.

    Sql Server Express must be installed on the machine.
    The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.

    So I don't have the Express version of SQL Server on my machine. The first thing I have to do is open the Web.config file and change the connection string from this

    1. <connectionStrings>
    2.         <add name="ApplicationServices"
    3.             connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
    4.         User Instance=true" providerName="System.Data.SqlClient"/>
    5.     </connectionStrings>

    to this

    1. <connectionStrings>
    2.         <add name="ApplicationServices"
    3.             connectionString="data source=.;Integrated Security=SSPI;Initial Catalog=aspnetdb"/>
    4.     </connectionStrings>

    Now let's try again by clicking on the login page. Still a problem for me, now I get the following error

    Cannot open database "aspnetdb" requested by the login. The login failed.
    Login failed for user 'Denis-PC\Denis'.

    Okay this makes sense since I don't have this database, I need to create it. There are 2 ways you can do this. Either navigate to C:\Windows\Microsoft.NET\Framework\v2.0.50727 and execute aspnet_regsql.exe or run the Visual Studio command prompt.

    Start --> All Programs --> Microsoft Visual Studio 2008 --> Visual Studio Tools --> Visual Studio 2008 Command Prompt

    After the Visual Studio Command Prompt is up type aspnet_regsq and hit enter

    Below is a screenshot of what it looks like if you run it from the command prompt

    Click Next on the first screen of the wizard, this will bring you to the screen shown below

    Leave the top option selected and click Next, this will bring you to the screen shown below

    In the Server field type your SQL Server name or type a dot if it is on your local server, type a username and password if you use sql authentication or select windows authentication if you use that. Leave the last option as it is and click Next. Now you will see a summary screen, click Next on the summary screen and then if everything went as it should you should see a Database has been created or modified screen, click finish to exit the wizard.

    Now you can go back to your ASP.NET MVC site, hit F5 in Visual Studio and click Log On, click on the register link, fill out the fields and you should see something like the screen below.

    Congratulations, you have now completed a small step toward you first ASP.NET MVC site, you now have a functional site but you need to add Models, Controllers and Views.

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    11285 views
    InstapaperVote on HN

    4 comments

    Comment from: Alex Ullrich [Member] Email
    Alex Ullrich A lot of pictures here. Did Ted ghost write it for you?
    01/17/10 @ 09:30
    Comment from: SQLDenis [Member] Email
    SQLDenis Haha, no I decided to try to give him a run for his picture money
    01/17/10 @ 12:49
    Comment from: Terry [Visitor]
    Terry Hey Alex go easy on the pics. It's a good thing. :) Remember the old saying:
    "A picture is worth a thousand words."
    01/25/10 @ 09:00
    Comment from: Tiggy [Visitor]
    Tiggy Fantastic! I was so not confident it would work because I saw another website said to modify this and that and since this was so straightforward, i thought surely it would fail! But it worked! Thanks a bunch! The pictures are worth a thousand words - suited for novices.
    03/02/12 @ 01:02

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)