views:

316

answers:

2

mdf file is working correctly under App_data folder but after attaching it to sql server give following error when running asp.net page.

Cannot open user default database. Login failed. Login failed for user 'Domain\myUserName'.

[edit] More information; SQL data source and connection string.

<asp:SqlDataSource
        id="srcFiles"
        ConnectionString="Server=.\SQLExpress;Integrated Security=True;
            AttachDbFileName=|DataDirectory|FilesDB.mdf;User Instance=True"
        SelectCommand="SELECT Id,FileName FROM Files"
        InsertCommand="INSERT Files (FileName,FileBytes) VALUES (@FileName,@FileBytes)"
        Runat="server">
        <InsertParameters>
            <asp:ControlParameter Name="FileName" ControlID="upFile" PropertyName="FileName" />
            <asp:ControlParameter Name="FileBytes" ControlID="upFile" PropertyName="FileBytes" />
        </InsertParameters>
    </asp:SqlDataSource>
A: 

You can make sure that you are specify the correct sql username and password or you can give 'Domain\myUserName' access to your database. check out the connection strings below.

Connection string samples

ps
you are specify = you specify
ps
please review my connection string code.
Novice Developer
EXEC sp_grantlogin 'Domain\myUserName'USE YOURDBNAMEEXEC sp_grantdbaccess 'Domain\myUserName'you should check out the following linkhttp://forums.asp.net/t/1296417.aspx
ps
A: 

I'm not sure how an MDF file is "working correctly" if it is just sitting in a folder and not attached to SQL Server. What do you mean by "working correctly"?

Anyway, what version of SQL Server? You can use sp_defaultdb to set the default database for that login to another database that you know is there, e.g.:

EXEC sp_defaultdb @loginame = 'Domain\myUserName', @defdb = 'tempdb';

TechNet: sp_defaultdb

If you are using SQL Server 2005 or 2008, you should use ALTER LOGIN instead, e.g.

ALTER LOGIN [Domain\MyUserName] SET DEFAULT_DATABASE = tempdb;

TechNet : ALTER LOGIN

Aaron Bertrand