We help IT Professionals succeed at work.

Changing binding source connection

Moe DeShong
Moe DeShong asked
on
Once a binding source is created the connection string is placed as an application setting in the project settings. That's fine while I'm developing. Once I install on a different computer the database location changes and the binding source no longer has the right connection information.  How do I let the binding source know where the new data location is?
Comment
Watch Question

what is your application? windows or web?
if web that should be in the web.config file
otherwise search in app.config

Author

Commented:
It's a windows app.  Code?  Need more info.
Most Valuable Expert 2011
Top Expert 2015

Commented:
That's the beauty of configuration files:  all you should have to do is change the part of the config file that changes between systems. In your case, you would change the "Data Source" property to point to the new server. Your database name (Initial Catalog) should be the same.

Author

Commented:
Ok, but how do you do via code?

Author

Commented:
I've never worked with the config file so don't know how to look it up and make the necessary changes.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Try accessing it using My.Settings.settingname but to be able to change it, you need to change its scope to User.
Can't change it to user.  It is locked.  

I just notice the tableadapter has a connection property.  Would something like this work?:

AppointmentsTableAdapter.Connection = Nothing
AppointmentTableAdapter.connection = "My connection"?

Author

Commented:
I would assume it would have to be placed in the formload event?

Author

Commented:
Answered my own question.  

AppointmentsTableAdapter.Connection = Nothing
AppointmentTableAdapter.connection = "My connection"?

Works perfect.  Thanks for the input.  I'm a newbie with the table adapters.  Always wrote my own sql.  
TableAdapters do make short work of coding.

Author

Commented:
Figured it out a posted my solution.
Most Valuable Expert 2011
Top Expert 2015

Commented:
But why would you want to change it via code? Changing the connection via code means you end up having to do something like this (pseudo-code):

if (production)
{
    connection = prod_conn;
}
else if (qa)
{
    connection = qa_conn;
}
else if (dev)
{
    connection = dev_conn;
}

Open in new window


What happens if the prod server goes out of commission, and you have to load the program onto a new server? Now you have to locate this place in code, and you have to change the prod code to point to the new server. Then you have to recompile the app, then redistribute/install it. If you put the connection info in your configuration file, then you only have to open a text file, make the change, save the file, then run the program.
Most Valuable Expert 2011
Top Expert 2015

Commented:
and you have to load the program onto a new server?
That should have said, "and you have to load the DB onto a new server?"
Most Valuable Expert 2012
Top Expert 2014

Commented:
Or

AppointmentTableAdapter.connection.ConnectionString = "new connection string"