0

This question may have been asked and answered before, but I do not know how to search for it.

The situation:

I have a Winforms application with many bound controls. We have four testing environments and currently, I have been connecting to each data source and importing all data tables and stored procedures using the Data Source Configuration Manager then having the connection string information stored in my app.config file. When I call the connection string, I am using My.Settings.[connection string name].ToString (my first problem).

For all data bound controls (mostly datagridview), I remove the old data set + the table adapter and all binding sources before dragging the new data set + table adapter and binding sources. I have a Public Sub that clears and rebinds all of my controls, but the code calls for the specific name of each data source (see below for example).

Public Sub SetRegionDEBindings(ByVal errorCode As String, ByVal Region As String)
    Try
        Dim selectLOMAbyRegion As New BindingSource
        selectLOMAbyRegion = Me.SpSelectLOMABindingSource

        Me.SpSelectLOMATableAdapter.Fill(Me.PrdGISDataSet.spSelectLOMA, errorCode, Region)

        Me.dgDE.DataSource = selectLOMAbyRegion
        Me.bnDE.BindingSource = selectLOMAbyRegion

        Me.txtDEIssueDte.DataBindings.Add(New System.Windows.Forms.Binding("Text", selectLOMAbyRegion, "IssueDte", True))
        Me.txtDECaseNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", selectLOMAbyRegion, "CaseNum", True))
        Me.txtDECommNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", selectLOMAbyRegion, "CommNum", True))

        With Me.dgDE
            Me.DataGridViewTextBoxColumn1.Name = "DERegion"
            Me.StateDataGridViewTextBoxColumn.Name = "DEState"
            Me.LatitudeDataGridViewTextBoxColumn.Name = "Latitude"
            Me.LongitudeDataGridViewTextBoxColumn.Name = "Longitude"
            Me.IdLOMADataGridViewTextBoxColumn.Visible = False
            Me.DiskNumDataGridViewTextBoxColumn.Visible = False
            Me.DataGridViewTextBoxColumn1.Visible = False
            Me.InsertDteDataGridViewTextBoxColumn.Visible = False
            Me.LOMAStatusDataGridViewTextBoxColumn.Visible = False
            Me.NotesDataGridViewTextBoxColumn.Visible = False
            Me.StateDataGridViewTextBoxColumn.Visible = False
            Me.SupercedeDataGridViewCheckBoxColumn.Visible = False
            Me.UpdatedDteDataGridViewTextBoxColumn.Visible = False
        End With

        Me.dgDE.Visible = True
        Me.dgDE.Rows(0).Selected = True
        Me.dgDE.BringToFront()

        If Me.dgDE.Rows(0).Cells("LOMAStatusDataGridViewTextBoxColumn").Value.ToString = "2" Then
            MessageBox.Show("Warning: this case number has been previously inserted.")
        End If
    Catch ex As Exception
        MessageBox.Show("Error setting data entry bindings: " & ex.Message.ToString)
    End Try
End Sub

My questions:

  1. Should I create my app.config file from scratch?

  2. If I need to change a server environment, can I just change the app.config file (and call Configuration.ConfigurationManager instead of My.Settings...)?

  3. If I change the app.config file, how would I re-bind my data sources without opening the solution, re-creating the data sources, and cleaning/re-building? (Because currently, I connect to the new data source, drag the binding sources onto my control, and update the dataset code manually)

I have looked here:

How To Change The Connection String saved in My.Settings in VB 2010

https://msdn.microsoft.com/en-us/library/ms171889(v=vs.90).aspx

https://social.msdn.microsoft.com/Forums/en-US/7483b816-be7a-4204-a4d3-cfb14b2aae26/how-to-dynamically-change-connection-string-in-generated-dataset-class?forum=adodotnetdataset

But, like I said, I'm not sure if I'm searching for the right answer.

Community
  • 1
  • 1
alybaba726
  • 390
  • 4
  • 14
  • What's the problem exactly and what do you want to do? – Reza Aghaei Nov 10 '15 at 17:44
  • I need to be able to change the connection string in the app.config file and have the data sources within the application re-bind to that connection string automatically (without going in to the solution, dragging the correct data source elements on to my controls and cleaning/rebuilding) – alybaba726 Nov 10 '15 at 17:56

1 Answers1

1

Change Connection String at Run-Time

To change connection string dynamically at run-time you should apply some small changes in your application. (I Suppose the name of your connection string is MyMainConnectionString)

  1. Change the MyMainConnectionString at your Setting file.

    • Open settings in designer
    • Select the connection string value and copy it for example Data Source=(localdb)\v11.0;Initial Catalog=TestDB;Integrated Security=True
    • Change the Type to string
    • Change the Scope to User
    • Paste the copied value to Value
  2. From now on when you want to add a TableAdapter to your DataSet in the wizard, choose to not save the connection string.

Now you have a normal string setting MyMainConnectionString that can be changed at run-time and can be saved.

You can set the value this way:

My.Settings.MyMainConnectionString= "Some New Value"

The value also can be set using some other predefined properties like for example DebugConnectionString, TestConnectionString, ProductionConnectionString and set this value based on some criteria or other setting this way:

My.Settings.MyMainConnectionString = My.Settings.TestConnectionString

To save the settings, you can use:

My.Settings.Save()


Change Connection String at Design-Time

You can simply change the value of your connection string in Setting.settings file.


Change Connection String for Deployed Application

You can change the value of connection string in yourApplication.exe.config.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • When you say "Settings" file, you mean App.config or another file? – alybaba726 Nov 10 '15 at 18:22
  • No I don't mean app.config I mean the `Settings.settings` In solution expand `My Project` node and double click on `Settings.settings` to open it. – Reza Aghaei Nov 10 '15 at 18:26
  • I wouldn't need to change the connection string once the application executes, I'd need the ability to change it before the application executes. But I could apply the same idea to the App.config file, correct? – alybaba726 Nov 10 '15 at 18:27
  • **If you want to change the value at run-time** I think this solution is the ultimate solution that makes changing connection strings really easy. **If you want to change the value at build/debug time** You can simply change the value of that property using settings file. **If you want to change the value for an application that is distributed** You can change yourApplication.exe.config. – Reza Aghaei Nov 10 '15 at 18:34
  • The important common point in those 3 solutions id you don't need to change app.config manually. – Reza Aghaei Nov 10 '15 at 18:35
  • Additional to this answer, you may want to read my post here about [changing default settings](http://stackoverflow.com/a/32532720/3110834) that is applicable only on deployed applications. – Reza Aghaei Nov 10 '15 at 18:37
  • Let me know if you this answer and the other answer is helpful :) – Reza Aghaei Nov 10 '15 at 18:37
  • This is VERY helpful, the main issue is if our production servers have to go into disaster recovery, so via the solution may not be the best option (it would require me (the developer) to be here to make the change). Our tech support has a process put in place already to change the App.config files of our existing applications when this happens, I just want to follow suit. – alybaba726 Nov 10 '15 at 18:53
  • I will continue to look into your answer--I do believe you've given me the resources I need. I'll mark it as the answer as soon as I can confirm. Thank you so SO much. – alybaba726 Nov 10 '15 at 18:54
  • Thank you for your feedback, you can vote for answers too when you find them helpful :) – Reza Aghaei Nov 10 '15 at 18:55
  • About the disaster, there are many solutions that don't need the developer, **for example** you can create a setting form and let the user to change the settings values and save the settings **or** you can use a mirror server in connection string **or** ... – Reza Aghaei Nov 10 '15 at 18:56
  • It would be great if you find the answer acceptable :) – Reza Aghaei Nov 17 '15 at 15:04
  • Unfortunately the correct answer was you can't rebind data sources in a WCF application from the app.config file. The clean/build solution is the only option, but thank you for your help. – alybaba726 Nov 17 '15 at 19:34
  • Thank you for your feedback. My answer is general and based on your question is about windows forms. And based on my experience is completely true in windows forms. I don't have any idea about why you mentioned **wcf** in this comment? – Reza Aghaei Nov 17 '15 at 19:49