i m using Microsoft VS 2008 and in that VB.NET as the language
the problem is i m not able to store connection string every time machine changes have to keep on changing the connection string


i have searched browsed, surfed done everything but still not able to find the proper answer

please help!!!

thank you.

Dani AI

Generated

Quick answer and a reliable pattern to stop editing the string on every machine: put the connection string in your app configuration (not hard‑coded), use a machine‑independent placeholder for file paths, and read/construct the real path at startup from one place. was on the right track suggesting the app or user folders and ’s idea of a single shared value is useful — centralize creation/reading of the string in one helper so you only change code in one spot. (learn.microsoft.com)

Store the logical connection in the app.config and use the |DataDirectory| token for file-based DBs. Example app.config fragment:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="MyDb"
         connectionString="Data Source=|DataDirectory|\mydb.mdf;Integrated Security=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

At program start set DataDirectory to the folder you want (app folder, user profile, etc.), then read the named string with ConfigurationManager. This avoids hard paths and keeps one canonical source for the name. (learn.microsoft.com)

If you need the value editable and persisted per machine, use a user‑scoped setting (Project → Properties → Settings, Scope = User). Update and persist it at runtime with My.Settings and Save(). For global edits you can programmatically update the exe config with ConfigurationManager.OpenExeConfiguration and save, but note write/permission problems if the app is installed to Program Files — in that case change at install time or use an external config file. (learn.microsoft.com)

Practical checklist: centralize the logic in one module/helper, prefer |DataDirectory| for DB files, use user‑scope settings for per‑machine changes, ensure a reference to System.Configuration, and if editing the exe config at runtime handle permissions or use an installer to set the value.

Recommended Answers

All 7 Replies

How do you store your connection string?
There are a number of ways to store the connection string and it some of them you can include it to the project (settings, xml or ini file, or even registry values that you create in your code if they are not present).

yes it is there but everytime the path changes so is the connection string so cant keep changing all the time its a lenghthy project so what to do ?

Declare connection string in module level and consume in all the forms.If at all it changes u need to change only in the module not in all the forms.

didn't got u ?

windows application i am using modules and consume?please elaborate

If the path is relevant to where your app is stored, then you can use the

Application.StartupPath()

or if your are looking for user's folder you can use

System.Environment.SpecialFolder.Personal

and navigate yourself from there.
What is the connection string used to connect to?

ok thankx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.