dharav dharav - 1 month ago 24
SQL Question

SqlDependency.Start() gives error Cannot attach the file 'C:\Program Files (x86)\IIS Express\EduTech.mdf' as database 'EduTech'

I am trying to start

sql
dependency but it gives me the following error:


Cannot attach the file 'C:\Program Files (x86)\IIS Express\EduTech.mdf' as database 'EduTech'.


Full error details follow:


Cannot attach the file 'C:\Program Files (x86)\IIS Express\EduTech.mdf' as database 'EduTech'.

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 attach the file 'C:\Program Files (x86)\IIS Express\EduTech.mdf' as database 'EduTech'.

Source Error: Line 25: BundleConfig.RegisterBundles(BundleTable.Bundles);

Line 26: SqlDependency.Stop(con);

Line 27: SqlDependency.Start(con);

Line 28: }

Line 29: protected void Session_Start(object sender, EventArgs e)

stack trace[SqlException (0x80131904): Cannot attach the file 'C:\Program Files (x86)\IIS Express\EduTech.mdf' as database 'EduTech'.]


Here's what my connection string looks like:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\EduTech.mdf;Initial Catalog=EduTech;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>


I'm using it in my controller as follows:

string conStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;


When I remove the line
SqlDependency.start()
the app runs perfectly fine.

Answer

i solved myself by replacing default connection sting to

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=W:\ongoing projects\Edutech\EduTech\App_Data\EduTech.mdf;Initial Catalog=EduTech;Integrated Security=True" providerName="System.Data.SqlClient" />  

i don't know why DataDirectory doesn't work but specifying the exact location works