System.Data.SqlClient.SqlException: Cannot open database " " requested by the login. The login failed.

  • Dim sqlConnect As New SqlConnection()

    sqlConnect.ConnectionString = "Initial Catalog=netGuest; data source=(local); integrated security=True; connect timeout=120;"

    Dim sql As String = "CREATE DATABASE ON (NAME = 'netGuest.dat', FILENAME = 'App_Data/netGuest.mdf') LOG ON (NAME = 'netGuest.dat', 'App_Data/netGuest.ldf')"

    sql += "INSERT INTO Guestbook (Name, Email, URL, Comment) VALUES('Logan', 'logan_young@hotmail.com', 'http://www.astralwd.co.za', 'Thanks', 'Intro message here')"

    sql += "INSERT INTO Admin (Name, Pass) VALUES('" & txtAdminName.Text & "','" & txtAdminPass.Text & "')"

    Dim command As New SqlCommand(sql, sqlConnect)

    sqlConnect.Open()

    command.ExecuteNonQuery()

    sqlConnect.Close()

    I don't know if my problem is in the code above, or on my server, but when I run this code IIS generates the following

    Cannot open database "netGuest" requested by the login. The login failed.

    Login failed for user 'W-LYOUNG01\ASPNET'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: Cannot open database "netGuest" requested by the login. The login failed.

    Login failed for user 'W-LYOUNG01\ASPNET'.

    Source Error:

    Line 39: Dim command As New SqlCommand(sql, sqlConnect)

    Line 40:

    Line 41: sqlConnect.Open()

    Line 42: command.ExecuteNonQuery()

    Line 43: sqlConnect.Close()

    with the following satck trace:

    [SqlException (0x80131904): Cannot open database "netGuest" requested by the login. The login failed.

    Login failed for user 'W-LYOUNG01\ASPNET'.]

    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +735075

    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188

    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838

    System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +33

    System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +628

    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.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +28

    System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +424

    System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66

    System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +496

    System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +82

    System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105

    System.Data.SqlClient.SqlConnection.Open() +111

    Install_one.btnInstall_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Guestbook\install.aspx.vb:41

    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105

    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107

    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7

    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11

    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33

    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

    I've been working on this for nearly a week now and I'm getting desperate!!!

    Any suggestions will be greatly appreciated. I figure that if I could add sysadmin or dbcreator privileges to the user in question, this would work, but I don't know how to do that...

  • Your problem is with your connection string, NOT your SQL commands (though I think that there are some problems there too).

    Connection String:

    You Initial DB (or "Catalog") is 'netGuest', but since you are creating it in your SQL statements, I am guessing that it does not exist yet. This will certainly cause your Login to fail. Set your "Initial Catalog" to some other DB that you know exists, like 'master'.

    CREATE DB command:

    Your data file and your log file have the same logical name, which I am pretty sure is not allowed.

    Ending logical files names with '.dat' seems like a bad idea to me, but you can probably make it work.

    INSERT commands:

    After you create the database, THEN you will want to switch your context to it before executing SQL statements against it.

    However, if you just CREATEd the database, then there are no user tables yet.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply