Help with Connection String

gaby@mrhs.ent

Member
Joined
Jan 29, 2010
Messages
6
Programming Experience
1-3
I been working on a Printer INK Management Program for my Company. We have many different districts we deal with. Each district has a different Server.

I am trying to create the application where they can place the database file anywhere they would like on the server and when they first run the application it would ask them to choose the location of where they placed the file.

I would like to know how i would be able to edit the connection string information in the app.config file during run time.

Otherwise if there is a better solution PLEASE HELP! I am in desperate need of help.

Thanks.
 
Last edited:
yes the theory sounds great and that is what i am trying to acheive however, the code that they supplied which is

"
If you have an application named test.exe and the following connectionStrings section inside your App.config:


Code Snippet

<connectionStrings>

<add name="Test" connectionString="Data Source=ServerName;Integrated Security=True"/>

</< FONT>connectionStrings>



The following code will modifiy the Test connection string:



Code Snippet

Configuration config = ConfigurationManager.OpenExeConfiguration("test.exe");

ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");

connectionStringsSection.ConnectionStrings["Test"].ConnectionString = "Data Source=ServerNameChanged;Integrated Security=True";

config.Save();
"


However, VB.NET is not recognizing any of those commands.. What do i need to do???
 
what are you talking about lol? there is no i variable used? the configure commands are unrecognized.
C#:
Type Varname = Value;
VB
Dim Varname As Type = Value

in your case:
Configuration config = ConfigurationManager.OpenExeConfiguration("test.ex e");
goes to:
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration("yourappnamegoeshere")
 
thanks that definitely helped with that part...

I am still getting some errors before compiling.
for these syntax

"
ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");

connectionStringsSection.ConnectionStrings["dbLocationConnectionString"].ConnectionString = "Data Source=ServerNameChanged;Integrated Security=True";
"

error is "'ConnectionStringsSection' is a type and cannot be used as an expression."
 
Why don't you write a small manual for the person who installs the site locally to edit the app.config manually? The app.config is meant to be in the first place to edited by a person, not a machine.

If someone can install your application somewhere, he or she should be capable editing the app.config.

Otherwise, you can always to make the choise to define one central database, containing a table with system variables for each location. Every installation uses the same database to get their connection string on a database you can control. The connection to the local database will be made programatically.

Be sure to shield the central database on IP. You can even make some web service, only callable from specific ip-addresses, return connection information based on the calling IP-address.

--- Joris Bijvoets ---
Author of Developers Challenges
 
Please use CODE tags when posting code
Visual Basic .NET Forums - BB Code List

VB.NET:
ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("conne ctionStrings");

connectionStringsSection.ConnectionStrings["dbLocationConnectionString"].ConnectionString = "Data Source=ServerNameChanged;Integrated Security=True";
This is C# code. Convert it to VB either by brain, or by something like the developer fusion converter (google it) giving something like:

VB.NET:
Dim connectionStringsSection as ConnectionStringsSection = DirectCast(config.GetSection("conne ctionStrings",ConnectionStringsSection);

connectionStringsSection.ConnectionStrings("dbLocationConnectionString").ConnectionString = "Data Source=ServerNameChanged;Integrated Security=True";
 
i still receive an error when trying to compile.

Dim connectionStringsSection as ConnectionStringsSection = DirectCast(config.GetSection("connectionStrings", ConnectionStringsSection))

Syntax error in cast operator; two arguments separated by comma are required.
 
Back
Top