1

I have a project in which I have a report with the dataset and everything. The problem is that when I want to deploy it, it throws an exception (in other computers) saying that it cannot open the database (I'm using SQLite) and that is because the connection string created by the server explorer is like:

C:\<path to my visual studio project>\bin\release\database.db

How do I manage the connection string properly for the reports?

peakxu
  • 6,667
  • 1
  • 28
  • 27
Hikaros
  • 101
  • 1
  • 12
  • Well, where is the database file deployed? – CL. Aug 08 '14 at 07:27
  • It is created whenever needed in the directory where the application is. – Hikaros Aug 08 '14 at 14:22
  • So your program knows exactly what the correct connection string would be? – CL. Aug 08 '14 at 15:23
  • haha well i create the database with `conn = new SQLiteConnection("Data Source=Facturacion.db;Version=3;New=True;Compress=True;");` so i don't really define any path, it just creates the database wherever the .exe file is. x_x – Hikaros Aug 08 '14 at 15:29
  • So you are asking how you can find out the path of your .exe? – CL. Aug 08 '14 at 15:30
  • No, i can get that path, but the problem is that even if i can get that path, i don't know how to tell my report to look for the database there. When i'm making the project the path to the project folder is the one i posted so when i create the setup package or take just the release folder and put it in another computer it throws an unhandled exception saying that it couldn't open the database because it is trying to access the database in my computer's path, where the project is. http://pastebin.com/mg16a97N – Hikaros Aug 08 '14 at 15:51
  • If i create this path `C:\\bin\release\ ` and put the files inside the release folder the program will work. – Hikaros Aug 08 '14 at 15:52
  • On a side note, i'm saying the report because the exception comes up when it is trying to fill the report. The problem itself is most likely that i made the connection to the database (to get the table so i could drag it to the dataset and then use it for the report) using the Server Explorer and the Connection string is: `data source=C:\fact\fact\bin\Release\database.db` so i want to tell the application to change that to a dynamic but i have no idea how :C – Hikaros Aug 08 '14 at 15:58

1 Answers1

0

A few days after i gave up on this i accidentally found where the connection string is being stored. The connection string is stored in a file called app.config, from there i just did some research and did what one answer said in this question: ConfigurationManager.AppSettings - How to modify and save?

Basically what i had to do to solve my issue was:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["<project_name>.Properties.Settings.<my connection string>"].ConnectionString = "data source=" + Application.StartupPath + "\\database.db";
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
Community
  • 1
  • 1
Hikaros
  • 101
  • 1
  • 12