My Little Help Desk - ASP Help Forum
My Little Help Desk - ASP Help Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
 All Forums
 My Little Help Desk - ASP Help
 Active Server Pages
 change an object data source connection string
 New Topic  Reply to Topic
Author Topic  

Administrator

28 Posts

Posted - 05/08/2008 :  10:53:55  Show Profile  Reply with Quote
To change an object data source connection string dynamically at runtime, try this:

We'll use "ods" as the object name for this example.


Protected Sub ods_ObjectCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceEventArgs) Handles ods.ObjectCreated
If e.ObjectInstance IsNot Nothing Then
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("newconnstring").ConnectionString
e.ObjectInstance.GetType.GetProperty("Connection").SetValue(e.ObjectInstance, conn, Nothing)
End If
End Sub

You must also set the "ConnectionModifier" of the table adapter for the object to Public.

In this example, the new connection string is stored in web.config, as "newconnstring".

Good luck!

Administrator

28 Posts

Posted - 08/28/2009 :  14:56:18  Show Profile  Reply with Quote
I know ... isn't this answer a life-saver! Such a simple thing, but Microsoft makes it so hard to find the answer for how to do this.

Enjoy! Leave me a note if it helped!
Go to Top of Page
  Topic  
 New Topic  Reply to Topic
Jump To:
My Little Help Desk - ASP Help Forum © mylittlehelpdesk.com Go To Top Of Page
Snitz Forums 2000