We help IT Professionals succeed at work.

Cannot acces .mdf  (mssql 2005 express) in Visual Studio Express project (c#)

Medium Priority
Last Modified: 2013-11-27
Well, I can browse it within VSE, but when i hit F5 this error msg appears...

" Cannot open database "MyChars" requested by the login. The login failed. Login failed for user 'Brunte\David'. "

This is a school example i need to get running in order to learn asp.net... :-)

Let's say php/mysql is easier to get goin'...

I'm using Vista Buisness (swedish), IIS7, Visual Web Developer Express 2005 (updated for vista), MS Sql Server 2005 Express.

Beskrivning: Ett undantag som inte kunde hanteras uppstod när den aktuella webbegäran kördes. Mer information om felet och var i koden det uppstod finns i stackspårningen.

Undantagsinformation: System.Data.SqlClient.SqlException: Cannot open database "MyChars" requested by the login. The login failed.
Login failed for user 'Brunte\David'.


Ett undantag som inte hanteras genererades vid körningen av den aktuella webbegäran. Information om undantagets ursprung och plats kan identifieras med undantagsstackspårningen nedan.  


[SqlException (0x80131904): Cannot open database "MyChars" requested by the login. The login failed.
Login failed for user 'Brunte\David'.]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +736211
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1959
   System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +33
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +237
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +374
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +192
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +170
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +359
   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +27
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +47
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105
   System.Data.SqlClient.SqlConnection.Open() +111
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +121
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +137
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +83
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1770
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +17
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +41
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360


Versionsinformation: Microsoft .NET Framework-version:2.0.50727.312; ASP.NET-version:2.0.50727.833
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)
        //Declare and define a connection string
        string strConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyChars.mdf;Integrated Security=True;User Instance=True";
        //Declare a refernce to SqlConnection and create an obiect "Data Source=.\SQLEXPRESS;|" '
        SqlConnection conn = new SqlConnection(strConnectionString);
        //Declare a reference to the SqlCommand and create an object
        SqlCommand cmd = new SqlCommand("Select * from Cars", conn);
        //Declare a reference to the DataReader
        SqlDataReader objRead;
        //Open the connection to the database
        //Read and return a row 
        objRead = cmd.ExecuteReader();
        //Navigate througt the batabase
        while (objRead.Read())
            Response.Write(objRead.GetString(2) + "<br>");
        //Close the connection

Open in new window

Watch Question

Make sure the login u'r trying to use has read permissions or atleast added to access the database. The error you see is due to improper setup of permission to the database for the said login.


Yeah but how?
-I can't find any security settings for sql within VSE, exept "owner" (alreadey set).

-I've installed Management Studio Express, updated to MSE SP2. But on my Vista it chrashes whenever I try to Open a file.

-I can't Attach the file based db, 'cause it's not possible to browse to c:\users\david\documents\visual studio... !
(others have had the similar problem)

-In file security for mychars.mdf (chars means cars :-) I've already added Brunte\David, Everyone and
Brunte\IIS_IUSRS (replaces user ASPNET in Vista).

-I need to deliver my tasks in one single folder  anyway, so the db has to reside in the App_Data folder.

Thanks :-)



Aha U should connect to Sql Server Compact Edition, but I have no luck there either...
(see pictures)


Bah, I'll just stick to backup/restore. Works at least :-)