Connection string issue

I have created a project which gets its data from SQLExpress and used the Add Data Source wizard and then dragged and dropped to create data bound forms and TableAdapters. All works well on my machine.

Now I want to link to data on another machine on my LAN. If I manually alter the connection string created for me automatically in the Settings tab of my project and point it at another machine all works fine. However I want to be able to do this programatically as all my users wont have VBExpress running.

The trouble is that the connection string in the settings tab is read only as it is application scoped. I could create a user scoped connection string which can be saved but this wont show up in the TableAdapter property setting for the connection string. Catch 22.

Would I have to settle on a user scoped connection string and change the TableAdapter connection string property every time a form that uses a TableAdapter loads

Thanks for any help,

GS



Answer this question

Connection string issue

  • Greg Van Liew MSFT

    I've used the config file in VS.NET 2003 but I haven't had the pleasure yet in VB 2005, so I'm not 100% sure about the details that have changed. In 2003 you would set the ConnectionString as a dynamic property and it would then be saved in the config file. You would then be able to edit the config file by hand or programmatically at each installation to update the ConnectionString appropriately. VB 2005 has the My.Settings object that handles this functionality but more easily and with more power I believe. If you make the ConnectionString an ApplicationSetting it can be be loaded from and saved to the config file using the MySettings object. I can't be more specific I'm afraid. This is the way it's supposed to be done though, so I'd suggest that you read up on the My.Settings object.

  • Jools .Net

    I found it Dear GS:

    in the solution explorer, you should show all files by clicking: "Show All Files"

    let we say that your dataset is:

    YearsDataSet.xsd

    expand it, you should find YearsDataSet.Designer.vb. Open it:

    it contain the YearsDataSet class and a namespace called:

    Namespace YearsDataSetTableAdapters

    this namespace contain all TableAdapters Class. each TableAdapter in your dataset have its class here.

    each class have a sub that read the ConnectionString from the ReadOnly Setting you talk about.

    for example: in the class

    Partial Public Class CertificateTableAdapter

    you will find the sub:

    <System.Diagnostics.DebuggerNonUserCodeAttribute()> _

    Private Sub InitConnection()

    Me._connection = New System.Data.OleDb.OleDbConnection

    Me._connection.ConnectionString = Global.Schools.My.MySettings.Default.YearsConnectionString

    End Sub

    you can here change the line:

    Me._connection.ConnectionString = Global.Schools.My.MySettings.Default.YearsConnectionString

    to

    Me._connection.ConnectionString = My.MySettings.MyConnectionString

    while MyConnectionString is a setting that you create in your Project settings.

    Note that if you open the Dataset designer and change any things it may recreate the file and you should rechange the line.

    please tell us if this solve your problem



  • vaguy02

    This is how you do it.  Thanks NTDS.

    Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

    Me.Item("VBNMDATAConnectionString") = Me.Item("NewConnection")

    End Sub

    GS

    Re: How to dynamically change connection string in generated dataset class
    Was this post helpful

    I found a link on the forums:
    https://blogs.msdn.com/smartclientdata/archive/2005/07/25/443034.aspx

    Basically I had a situation where I wanted to change the connectionstring that M$ store when using the funky project data sources way of coding.

    All I did is added this sub to the settings.vb file (instructions how to view the setting code in included in that link above)

    Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

    Me.Item("cnDatabase") = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.Item("UserSettingDatabaseLocation")

    End Sub
     
    Where cnDatabase is the name of the Connection you have used for your datasets, and UserSettingDatabaselocation was just a User setting that i have used, but you could set that to anything like your own XML based setting or anything.

    Good Luck!


  • tq

    Hi GS.

    I had the same problem, but I solved it using another way.

    you should have the same database in another location. let we say each user had a copy of the main database but each one contain different data.

    My solution:

    create a string in the setting, and give it : "MyConnectionString" as name

    set it to User Type to be read and write. not read only.

    change the MyConnectionString as you need then:

    On each form load, just put all of your Tableadapter.connection.connectionstring=my.setting.MyConnectionString.

    put this line before the tableadapter fill the dataset.

    note that each database must have the same tables, fields, fields properties as the dataset or the main database where you create the wizard.

    may be we can change the connectionstring in the tableadapter itself, so we do not have to change it every form loading. I will see it later.



  • iAlbeniz

    Hi,

    Thanks for going to so much trouble. I will give it a go and let you know though I am a little nervous about changing the generated code.

    GS


  • Connection string issue