alybaba726
alybaba726

Reputation: 400

Re-binding controls by changin app.config

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.

Upvotes: 0

Views: 467

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125277

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.

Upvotes: 1

Related Questions