TableAdapter Partial Public Class - What code do you use to set the connection on fly

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

I wish to set the connection on the fly using the TableAdapter Partial Public Class because it's set up in the DataSet Editor.

Can you tell me what coding to use and how to call the coding? So far I double clicked on the TableAdapter in the editor and it shows this code:

VB.NET:
Namespace FormulaDataSetTableAdapters
    
    Partial Public Class FormulasTableAdapter
        Public Sub SetTableAdapterConnection()
            Dim strDatabasePath = FormMain.strApplicationPathFolderName

            ' Declare a connection string.
            '-----------------------------
            Dim strDatabaseConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                strDatabasePath & "\Nutrition.mdb"

        End Sub
    End Class
End Namespace

I already placed the 2 dim statements in there. I don't how how to do the rest of the coding and don't know were to call SetTableAdapterConnection() from.

FormMain.strApplicationPathFolderName comes from using this statement:

VB.NET:
Public strApplicationPathFolderName As String = System.Windows.Forms.Application.StartupPath

The purpose of all this is to use the database from the place where the application is installed.

Thanks.

Truly,
Emad
 
Property <propertyname> is 'ReadOnly' - How do I fix it?

Hi Everyone,

I hope I'm posting this in the correct area.

I'm trying to change the connection string of a connection string defined in the "Settings" tab of my application using code in setting.vb

I get an error telling me that property "NutritionConnectionString" is readonly.

Here is the code I placed into the "New" event:

VB.NET:
Namespace My
    
    'This class allows you to handle specific events on the settings class:
    ' The SettingChanging event is raised before a setting's value is changed.
    ' The PropertyChanged event is raised after a setting's value is changed.
    ' The SettingsLoaded event is raised after the setting values are loaded.
    ' The SettingsSaving event is raised before the setting values are saved.
    Partial Friend NotInheritable Class MySettings
        Dim strDatabasePath = FormMain.strApplicationPathFolderName

        ' Declare a connection string.
        '-----------------------------
        Dim strDatabaseConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
            strDatabasePath & "\Nutrition.mdb"

        Public Sub New()

            ' Use make sure the database name is comming from where the user installed it.
            '-----------------------------------------------------------------------------
            Global.Pet_Nutrition.My.MySettings.Default.NutritionConnectionString = strDatabaseConnection
        End Sub
    End Class
End Namespace

Is there any way to get strDatabaseConnection into Global.Pet_Nutrition.My.MySettings.Default.NutritionConnectionString ?

Thanks.

Truly,
Emad
 
My.MySettings("NutritionConnectionString") = strDatabaseConnection

Works for me inside the Form's Load event, I don't know about in the My namespace though.
 
Hi JuggaloBrotha,

Thanks for the reply.

Normally I use the "New" procedure to put that kind of code.

I did try it in there but it still says it's readonly.

Could you tell me what coding I need to place there so It's not readonly?

The attachment show what we tried.

Truly,
Emad
 

Attachments

  • is readonly.jpg
    is readonly.jpg
    488.7 KB · Views: 96
Hi,

I found that other people had the same problem and it was resolved by adding this line just before the .Fill statement.

VB.NET:
        'TODO: This line of code loads data into the 'FormulaDataSet.Formulas' table. You can move, or remove it, as needed.

        Me.FormulasTableAdapter.Connection.ConnectionString = strDatabaseConnection ' Over-ride default setting.
        Me.FormulasTableAdapter.Fill(Me.FormulaDataSet.Formulas, strFormulaName)

Truly,
Emad
 
You can do as you have done (but make sure that the setting of the ConnectionModifier property is set to something that makes your .Connection property accessible .. if ConnectionModifier is Private then you cannot say MyTableAdapter.Connection because the property is generated as private)

Or you can modify the setting type to be a User Scope / String instead of Application Scope / (Connection String) and this will allow you to alter the conenction string in the settings on the fly, with: My.Settings.WhateverConnectionString = "conn string goes here"
but be aware that attempting to use the dataset designer after you have changed the setting type will cause the designer to fail, complaining that the connection string property doesnt exist in the settings

You can also add a property to the Settings.vb partial class that allows you to write the property of the setting. Be aware that Application Scope properties are NOT saved when you say My.Settings.Save() so altering things in this way means that you cannot save conn string changes between runs of the application
 
Back
Top