in

C1 Community

ComponentOne Community is a free source for developers and help authors to collaborate and communicate.

Changing the connection string dynamically

Last post 01-09-2007 3:47 AM by C#_Frank. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 06-01-2006 9:02 AM

    Changing the connection string dynamically

    How?

    Regards


    Mike
  • 06-01-2006 10:33 AM In reply to

    Re: Changing the connection string dynamically

    <Ace Grace> wrote in message news:181795@test.componentone.com...
    > How?

    Look at C1DataViewSet.ConnectionOpening event.

    Alex

    >
    > Regards
    >
    >
    > Mike
    >
  • 06-01-2006 11:40 AM In reply to

    Re: Changing the connection string dynamically

    So if I have 20 C1DataViewSet objects, I have to set the connection string in all of them?
     
    Regards
     
     
    Mike
     
     
    <C1_AlexI> wrote in message news:181811@test.componentone.com...
    <Ace Grace> wrote in message news:181795@test.componentone.com...
    > How?

    Look at C1DataViewSet.ConnectionOpening event.

    Alex

    >
    > Regards
    >
    >
    > Mike
    >

    http://home.componentone.com/cs/forums/181795/ShowPost.aspx#181811

  • 06-01-2006 2:33 PM In reply to

    Re: Changing the connection string dynamically

    <Ace Grace> wrote in message news:181828@test.componentone.com...
    > So if I have 20 C1DataViewSet objects, I have to set the connection string
    > in all of them?

    In a common case yes, note that in general an each DataTable in a typed
    DataSet may have its own unique connection string.
    But usually a connection management can be simplified. After you created a
    typed DataSet, all DataTable (to say more exactly - their table adapters)
    share the same connection string whose definition is stored in an
    application's global Settings class, thus you may globally change by a line
    of code like this:
    Settings.Default["ConnectionStringName"] = "new connection string";
    , where "ConnectionStringName" is the name that you will find in the
    Connection property of any TableAdapter in a typed DataSet designer. This
    must be done before any C1DataViewSet that works against this DataSet has
    been created (more exactly before C1DataViewSethas been connected to this
    DataSet).

    Alex

    > Regards Mike <C1_AlexI> wrote in message
    > news:181811@test.componentone.com...<Ace Grace> wrote in message
    > news:181795@test.componentone.com...
    >> How?
    >
    > Look at C1DataViewSet.ConnectionOpening event.
    >
    > Alex
    >
    >>
    >> Regards
    >>
    >>
    >> Mike
    >>
    > http://home.componentone.com/cs/forums/181795/ShowPost.aspx#181811
    >
  • 06-02-2006 4:04 AM In reply to

    Re: Changing the connection string dynamically

    Thanks I will take a look.
     
    Regards
     
     
    <C1_AlexI> wrote in message news:181848@test.componentone.com...
    <Ace Grace> wrote in message news:181828@test.componentone.com...
    > So if I have 20 C1DataViewSet objects, I have to set the connection string
    > in all of them?

    In a common case yes, note that in general an each DataTable in a typed
    DataSet may have its own unique connection string.
    But usually a connection management can be simplified. After you created a
    typed DataSet, all DataTable (to say more exactly - their table adapters)
    share the same connection string whose definition is stored in an
    application's global Settings class, thus you may globally change by a line
    of code like this:
    Settings.Default["ConnectionStringName"] = "new connection string";
    , where "ConnectionStringName" is the name that you will find in the
    Connection property of any TableAdapter in a typed DataSet designer. This
    must be done before any C1DataViewSet that works against this DataSet has
    been created (more exactly before C1DataViewSethas been connected to this
    DataSet).

    Alex

    > Regards Mike <C1_AlexI> wrote in message
    > news:181811@test.componentone.com...<Ace Grace> wrote in message
    > news:181795@test.componentone.com...
    >> How?
    >
    > Look at C1DataViewSet.ConnectionOpening event.
    >
    > Alex
    >
    >>
    >> Regards
    >>
    >>
    >> Mike
    >>
    > http://home.componentone.com/cs/forums/181795/ShowPost.aspx#181811
    >

    http://home.componentone.com/cs/forums/181828/ShowPost.aspx#181848

  • 01-08-2007 8:52 AM In reply to

    Re: Changing the connection string dynamically

    Hello Alex,

    I tried to make a use of the ConnectionOpening event of the C1TypedDataViewSet in my project
    and got into the folowing problem.
    The event is not fired for the very first time the underlying .net TypedDataSet retrieves the data
    from the database. The event handler is assigned just after the initializing line of the C1TypedDataViewSet:

    c1DataViewSet_MySet = new C1DataViewSet_MySet();
    c1TypedDataViewSetConnectionOpening += newC1.C1DataExtender.ConnectionOpeningEventHandler( this.c1TypedDataViewSet_MySet_ConnectionOpening);

    The only code inside the handler is a MessageBox.Show("Test") call.
    My intention however was to change the connection string prior to any data access activity.

    There is a C1TrueDBGrid on the Form, bound to a C1DataView defined in c1TypedDataViewSet_MySet. The Grid displays the data without the event having been fired
    and so I get an error if any modification to connection string has been needed. Any data manipulation after this first time fires the event as expected.

    There are project level C1TypedDataViewSet(Version=2.0.20062.32) and ADO.NET DataSet 2.0 DataSet used.

    Do You have any ideas concerning this matter?
    Thanks a lot!
    Regards

    C#_Frank




  • 01-08-2007 11:07 AM In reply to

    Re: Changing the connection string dynamically

    Hi Frank
     
    As I understand the source of the problem is that the first connection to a data occurs in the typed viewset constructor, i.e. your event handler is being attached after the connection has been established.
    A couple of ways how to work around this problem:
    1) Define an event handler inside the typed viewset definition, as a part the typed viewset class. Yom may expose some public property somewhere in your application that is visible from within the typed viewset class that keeps a necessary connection info.
    2) Add a constructor to the typed viewset with a parameter of the ConnectionOpeningEventHandler type, and store the passed value in a local field, e.g.:
    private ConnectionOpeningEventHandler _connOpCallBack = null;
    public MyTypedViewSet(ConnectionOpeningEventHandler connOpCallBack): this()
    {
        _connOpCallBack = connOpCallBack;
    }
    Define the ConnectionOpening event handler inside the typed viewset with the codelike this:
    if (_connOpCallBack != null)
        _connOpCallBack(this, e);
     
    Then create a viewset instance by means of this constructor:
    MyTypedViewSet vs = new MyTypedViewSet(new ConnectionOpeningEventHandler(myHandler));
     
    Please let me know if neither of these suggestions are appropriate.
     
    Thanks,
    Alex
    <C#_Frank> wrote in message news:190374@test.componentone.com...
    Hello Alex,

    I tried to make a use of the ConnectionOpening event of the C1TypedDataViewSet in my project
    and got into the folowing problem.
    The event is not fired for the very first time the underlying .net TypedDataSet retrieves the data
    from the database. The event handler is assigned just after the initializing line of the C1TypedDataViewSet:

    c1DataViewSet_MySet = new C1DataViewSet_MySet();
    c1TypedDataViewSetConnectionOpening += newC1.C1DataExtender.ConnectionOpeningEventHandler( this.c1TypedDataViewSet_MySet_ConnectionOpening);

    The only code inside the handler is a MessageBox.Show("Test") call.
    My intention however was to change the connection string prior to any data access activity.

    There is a C1TrueDBGrid on the Form, bound to a C1DataView defined in c1TypedDataViewSet_MySet. The Grid displays the data without the event having been fired
    and so I get an error if any modification to connection string has been needed. Any data manipulation after this first time fires the event as expected.

    There are project level C1TypedDataViewSet(Version=2.0.20062.32) and ADO.NET DataSet 2.0 DataSet used.

    Do You have any ideas concerning this matter?
    Thanks a lot!
    Regards

    C#_Frank






    http://home.componentone.com/cs/forums/181869/ShowPost.aspx#190374

  • 01-09-2007 3:47 AM In reply to

    Re: Changing the connection string dynamically

    Hi Alex

    Thank You for the suggestions. As I've got nothing instance specific in this project,

    I attached the event handler in the typed viewset default constructor:

    public C1TypedDataViewSet_MySet()

    {

    this.ConnectionOpening += new ConnectionOpeningEventHandler(C1TypedDataViewSet_MySet_ConnectionOpening);

    InitializeComponent();

    }

    void C1TypedDataViewSet_MySet_ConnectionOpening(object sender, ConnectionOpeningEventArgs e)

    {

    e.Connection.ConnectionString = Properties.Settings.Default.ConnectionString1.Replace("encrypted_PWD", "decrypted_PWD");

    }

    So I can apply the required modification to the connection string before it would be too late.

    Thank You very much for Your help!

    Best regards,

    Frank

Page 1 of 1 (8 items)
Contact ComponentOne: 1.800.858.2739 ©1987-2008 ComponentOne LLC All Rights Reserved.