Getting this error when I run my project in new PC. TO avoid this I have to copy paste the new Connection string every time .. Is there any way to avoid that ... I have 3 different database and its very annoying O_O
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\Yoro\\Desktop\\WAPent 3.0 (1)\\WAPent 3.0\\WAPent 3.0\\WAPent 2.0\\WAPent 2.0\\App_Data\\LoginStuff.mdf;Integrated Security=True;User Instance=True");
Web Config code
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LoginStuff.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
In the web.config you are using |DataDirectory| which is a substitution for the path to the datadirectory. This is set by using
AppDomain.CurrentDomain.SetData("DataDirectory", newpath)
When you do not set |DataDirectory| it defaults to the App_Data folder when it is a web project. So looking at your code the path represented by DataDirectory probably does not contain the database file.
For more info about datadirectory have a look at this (older) article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With