1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

System.Data.SqlClient.SqlException

Discussion in 'SQL Server' started by arpit.jh001, Mar 23, 2012.

  1. arpit.jh001

    arpit.jh001 New Member

    Joined:
    Feb 15, 2012
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    hi,I am a beginner in asp.net.......when I am trying to run my application I am getting the following error..Any help will be appreciated.


    Server Error in '/Onexam.zip' Application.

    An attempt to attach an auto-named database for file C:\onlineexam.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    [FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif] 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: An attempt to attach an auto-named database for file C:\onlineexam.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    Source Error:

    Line 20: SqlConnection con; Line 21: con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFileName=C:\\onlineexam.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); Line 22: con.Open(); Line 23: string str; Line 24: str = "select * from State";
    Source File: c:\Documents and Settings\arpit\Desktop\Projects\Onexam.zip\register.aspx.cs Line: 22

    Stack Trace:

    [SqlException (0x80131904): An attempt to attach an auto-named database for file C:\onlineexam.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +118 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +379 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +3922 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +70 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +406 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +740 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +415 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +310 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +774 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +89 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +625 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +163 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +884 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +187 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +143 System.Data.SqlClient.SqlConnection.Open() +262 register.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\arpit\Desktop\Projects\Onexam.zip\register.aspx.cs:22 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +37 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +91 System.Web.UI.Control.OnLoad(EventArgs e) +127 System.Web.UI.Control.LoadRecursive() +92 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3365 [/FONT]
     
  2. arpit.jh001

    arpit.jh001 New Member

    Joined:
    Feb 15, 2012
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I have saved my database files in App_Data directory and I used following code for connection to database files
    Code:
    SqlConnection con;
    con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFileName=C://onlineexam.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    con.Open();
    
     

Share This Page