Go4Expert

Go4Expert (http://www.go4expert.com/)
-   SQL Server (http://www.go4expert.com/forums/sql-server-forum/)
-   -   System.Data.SqlClient.SqlException (http://www.go4expert.com/forums/systemdatasqlclientsqlexception-t28078/)

arpit.jh001 23Mar2012 15:41

System.Data.SqlClient.SqlException
 
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.

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:\\onlineex am.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\registe r.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.OnErro r(SqlException exception, Boolean breakConnection) +118 System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) +379 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +3922 System.Data.SqlClient.SqlInternalConnectionTds.Com pleteLogin(Boolean enlistOK) +70 System.Data.SqlClient.SqlInternalConnectionTds.Att emptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +406 System.Data.SqlClient.SqlInternalConnectionTds.Log inNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +740 System.Data.SqlClient.SqlInternalConnectionTds.Ope nLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +415 System.Data.SqlClient.SqlInternalConnectionTds..ct or(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +310 System.Data.SqlClient.SqlConnectionFactory.CreateC onnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +774 System.Data.ProviderBase.DbConnectionFactory.Creat ePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +89 System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection owningObject) +625 System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection owningObject) +163 System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection owningObject) +884 System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection owningConnection) +187 System.Data.ProviderBase.DbConnectionClosed.OpenCo nnection(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\registe r.aspx.cs:22 System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e) +37 System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(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

arpit.jh001 23Mar2012 16:09

Re: System.Data.SqlClient.SqlException
 
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();



All times are GMT +5.5. The time now is 01:56.