none
Dynamically changing ConnectionString in settings.settings or for all tableadapters? RRS feed

  • Question

  • I've been searching for awhile now.  Is this really a limitation of using the wizard to make table adapters, etc for you??

    Also, there is no base 'tableadapter' class or a way that I can see to quickly change all 'tableAdapters' on a form.

     

    Though the best solution would be to somehow overwrite the value in the settings on application load..  I want to pull the value from a method that will essentially be returning a dynamic value.

    Wednesday, October 10, 2007 9:05 PM

All replies

  • What exactly do you need to achieve? Remember that you also could use connection, commands and other ADO.NET classes directly without building TableAdapters.

    Thursday, October 11, 2007 10:05 AM
    Moderator
  • Thanks for the suggestion, but I'm not looking to convert the items.

     

    Imagine you've put about 20 tables in a dataset/xsd file and have a lot of autogenerated code.

    Now converting all of them to non autogenerated and writing a lot of it by hand would take a large amount of effort, right? Sad

    Thursday, October 11, 2007 1:35 PM
  • It is OK, but you still did not post your actual question. Do you need to know how to change connection string dynamically or is it something else?

    Friday, October 12, 2007 10:15 AM
    Moderator
  • We just finished doing this sort of thing a few days ago.  You can actually write to the settings.settings property and replace it with whatever your dynamic connection string is.  Syntax is something like

    Properties.Settings.Default.<PropertyName> = <connection string>;
    Properties.Settings.Default.Save();

    Your other option is to create the table adapter and then just assign the connection string to it's connection object, ie

    MyTableAdapter mta = new MyTableAdapter();
    mta.Connection.ConnectionString = <connection string>;

    We ended up building a static TableAdapterFactory class to handle this...pass in an int signifying the type of adapter to create, and the connection string and just call:

    MyTableAdapter mta = (MyTableAdapter)TableAdapterFactory.Create(TableAdapters.MyTableAdapter, connectionString);
    Monday, October 15, 2007 1:48 PM
  • VMazur, my question is the subject of this thread.

     

    racka4279, that property is ReadOnly.  I can go into the code and take that off and make a set portion, but it is autogenerated code and will be removed if changes are made to settings.
    Monday, October 15, 2007 4:05 PM
  • It doesn't have to be.  For each of the TableAdapters in your dataset, you can use the designer and set their modifier to public and then you can set it in code.  After that it doesn't matter if the dataset autogenerates again, as the modifier won't change unless you change it.

    Of course, if you don't want to do that, you can just modify the settings.settings value whenever you need to.


    edit:  the property is actually "ConnectionModifier", and gives you choices of private, internal, protected internal, protected and public.  We're only using the dataset from within the project it's defined in, so Internal works perfectly for us.  You'll need to set it according to the scope you're using in your project, obviously.
    Monday, October 15, 2007 4:27 PM
  • Thanks for the reply again.

     

    I meant that the Settings property areas you mentioned are ReadOnly.

     

    I could change the table adapters' connection strings in code, but everytime I use them on another form, they'd need to be updated :/  This might be the only route to go, as everything else is creating issues.

    Monday, October 15, 2007 8:38 PM
  •  tee_user5 wrote:
     

    I meant that the Settings property areas you mentioned are ReadOnly.


    They shouldn't be. Check out MSDN and do some googling, there's lots of stuff out there about modifying the settings.settings values at runtime.

    http://msdn2.microsoft.com/en-us/library/aa730869(vs.80).aspx



    I could change the table adapters' connection strings in code, but everytime I use them on another form, they'd need to be updated :/  This might be the only route to go, as everything else is creating issues.



    The connection string property only needs to be set once.  If you're passing the same object around, the property will stay set.  If you need to create the TableAdapters on multiple forms, I'd suggest a static TableAdapterFactory class that has access to the dynamic connection string.  That way all the work's done for you.
    Tuesday, October 16, 2007 7:50 AM
  • :/

    From the link given:

     

    "The crucial distinction between application-scope and user-scope settings is that user-scope settings are read/write at run time, and their values can be changed and saved in code. Application-scope settings are read only at run time. While they can be read, they cannot be written to. Settings with application scope can only be changed at design time, or by altering the settings file manually."

     

    The setting that is already in place is an application scope setting and changing its scope is not allowed right now.

    Tuesday, October 16, 2007 4:45 PM