C# Change Connection String upon deployment


I have built an WPF application. The application connects to SQL Server to retrieve some data. I wish to deploy my application on a friend’s computer. I have the exe file along with the msi file. I have a connection string in my settings.cs file that connects to my database, however my friends database will be in a different location to mine. What I would like to know is the best way to change the connection string when my friend goes to install the program on his computer?

Thanks for any help,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Since I don't use Windows installer for my software (I've found Inno to be exponentially better) I'm not entirely sure how to do this in the installer, but you can make the program detect on launch if the connection string has been set, and if not ask for it and store it.

The best way to do this would be to use the registry.

RegistryKey myKey = Registry.CurrentUser.CreateSubKey("Software\\MYPROGRAM\\");
            object result = myKey.GetValue("ConnectionString", null);
            string connectionStr = "";

            if (result == null)
                string conStr = Microsoft.VisualBasic.Interaction.InputBox("", "Enter connection string", "Default Text");
                myKey.SetValue("ConnectionString", conStr);
                connectionStr = conStr;
                connectionStr = myKey.GetValue("ConnectionString").ToString();

Open in new window

You can avoid having to create an entire new form to take the input by simply including the Visual Basic .NET library that Microsoft provides and using the InputBox class.

This code works by first attempting to read the value of "ConnectionString" from HKCU/Software/MYPROGRAM. If it can't read it, it will ask for the user to enter it, the nstore it in the registry. If it was read, it will simply store the value in connectionStr for you to use later.
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
I have a connection string in my settings.cs file that connects to my database,

** I had such requirement while developing software for my client. So what I did ?

First way:

1. Created a .xml file in some directory when software run with connection string details in it.

2. Read connection string from that file.

3. User was having option of modifying those details from software itself. That is you can present user with form to input new connection string.

Second way:

1. If it is meant for testing on your friend system then you can provide him configuration file with connection string.

2. And he can modify those values in connection string and your software can read those details from there.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.