views:

868

answers:

2

how to change connection string dynamically in object datasource in asp.net ?

+4  A: 
protected void ObjectDataSource1_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
    if (e.ObjectInstance != null)
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = MyConnectionManager.ConnectionString;
        e.ObjectInstance.GetType().GetProperty("Connection").SetValue(e.ObjectInstance, conn, null);
    }
}

I hope it helps.

Braveyard
A: 

I didn't get the above to work but this did:

  if (e.ObjectInstance != null)
  {
    ((ReportPrototype.ReleasedRatingsDataTableAdapters.RatingsViewTableAdapter)e.ObjectInstance).Connection.ConnectionString = ConfigurationManager.ConnectionStrings["RADSDataConnectionString"].ConnectionString;
  }

ObjectInstance is the table adapter which in my case was the type bound to the ObjectDataSource.