Saturday, February 19, 2011

How do you change a connection string dynamically in an object datasource in asp.net?

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

From stackoverflow
  • 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.

  • 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.

0 comments:

Post a Comment