0

I have a connection string inside my app.config file.

<connectionStrings>
    <add name="ConnString" 
         connectionString="Data Source=|DataDirectory|\STOCK.sdf; Persist Security Info=False;" 
         providerName="Microsoft.SqlServerCe.Client.3.5"/>
</connectionStrings>

But I have got the following error: when accessing it:

enter image description here

Note: previously I had problem saving the data, and I changed the .sdf property to "Do not Copy", after that this error is happening.

4
  • is it pointing at the correct location? check connectionstrings.com Commented Jul 3, 2015 at 8:47
  • Now I solved the problem, just I moved the stock.sdf file to Bin/Debu folder. Commented Jul 3, 2015 at 8:53
  • From the sound of it, you've told visual studio not to copy your database file into the Debug folder when you compile. Meaning the path above probably does not contain your stock database. Commented Jul 3, 2015 at 8:53
  • After the edit I think the question should be why can't the file be found after changing the Copy to output folder to Do Not Copy as it is apparent that changing this flag caused the error Commented Jul 3, 2015 at 9:00

2 Answers 2

4

After changing file output to "Do not Copy" your STOCK.sdf file will not be copied to \bin\Debug directory and finally program will not find that file.

If you select "Copy Always" than you DB file will be always overwritten when you start debug. That's why you probably had "saving issue" - you just getting new fresh DB copy when starting debug again.

While selecting "Copy if newer" you will be able debug fine...until you make changes (add table, record and so on) to your original (which you probably can see in Visual Studio) STOCK.sdf file.

Sign up to request clarification or add additional context in comments.

3 Comments

You are correct, early I have solved the problem by moving STOCK.sdf to Bin/Debug folder.
Now I have seen my saved data changes. So, copying it to debug folder is the right way of doing that?
Yes. But till you change DB structure. If you have stable schema you can manually copy DB and it will be fine. But I would use copy if newer while still making changes to DB. Just don't forget - when you change DB your program will use fresh empty database.
1

You should use Server Explorer in Visual Studio to get connection string. Refer here: http://www.codeproject.com/Tips/314772/Using-Visual-Studio-to-find-a-database-connection

Hope this help.

2 Comments

I guess there connection string Data Source will have the path to project path\DB.sdf. If so then few notes: 1. During debugging your program will insert test records to original database. 2. Don't forget to change path after deployment.
Thank you @Renuiz, your suggestion is very useful and recommended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.