How to change the WEB API connection string dynamically

To change the Web API data source connection string dynamically in the Bold Reports® ASP.NET Core Report Viewer, read the report data sources with ReportHelper and set new DataSourceCredentials in the OnReportLoaded method of the Web API controller.

With this option, you can modify the connection details of a web API data source at runtime without restarting your application or performing any additional configuration steps. Runtime changes give you greater flexibility in managing data sources and simplify connecting to various data sources.

When you change the web API data source connection details at runtime, you can switch between different data sources, update connection strings, and adjust other connection details as needed. You can then adapt Bold Reports® reports to changing data requirements.

Follow these steps to change the Web API connection string:

  1. The data source information is stored in jsonResult, and you can store it in a local property.

            private Dictionary<string, object> _jsonResult;
            public object PostReportAction([FromBody] Dictionary<string, object> jsonArray)
            {
                _jsonResult = jsonArray;
                return ReportHelper.ProcessReport(jsonArray, this, this._cache);
            }
  2. Use the JsonResult information with the ReportHelper.GetDatasource API to get the data source details of the reports. Then use DataSourceCredentials to change the connection string of the data source.

            public void OnReportLoaded(ReportViewerOptions reportOption)
            {
                List<DataSourceInfo> datasources = ReportHelper.GetDataSources(_jsonResult, this, _cache);
                foreach (DataSourceInfo item in datasources)
                {
                    RestAPIModel dataModel = null;
                    if (item.DataSourceName == "Customer")
                    {
                        dataModel = JsonHelper.Deserialize<RestAPIModel>(item.ConnectString);
                        string connectionString = "https://customerdemo.boldreports.com/corewebapi/api/customers";
                        DataSourceCredentials DataSourceCredentials = new DataSourceCredentials();
                        DataSourceCredentials.Name = item.DataSourceName;
                        DataSourceCredentials.UserId = null;
                        dataModel.URL = connectionString;
                        DataSourceCredentials.Password = null;
                        DataSourceCredentials.ConnectionString = JsonHelper.SerializeObject(dataModel);
                        DataSourceCredentials.IntegratedSecurity = false;
                        reportOption.ReportModel.DataSourceCredentials = new List<DataSourceCredentials>
                            {
                                    DataSourceCredentials
                            };
                    }
                }
            }
  3. The JsonHelper class contains the Deserialize and SerializeObject methods. Deserialize returns the deserialized object from a JSON string, and SerializeObject returns the JSON string representation of an object.

            internal class JsonHelper
        {
            internal static ObservableCollection<JsonSchemaInfo> PortSelectedTableToJsonSchemInfo(List<TableSchemaInfo> selectedTableSchema)
            {
                ObservableCollection<JsonSchemaInfo> selectedJsonSchemas = new ObservableCollection<JsonSchemaInfo>();
                selectedTableSchema.ForEach(tableSchema =>
                {
                    var schema = new JsonSchemaInfo
                    {
                        SchemaName = tableSchema.ColumnName,
                        FiniteArraySchemaType = tableSchema.FiniteArraySchemaType,
                        SchemaType = tableSchema.SchemaType,
                        IsAnonymousSchema = tableSchema.IsAnonymousSchema,
                        ValueType = tableSchema.ValueType,
                        InnerArrayCount = tableSchema.InnerArrayCount,
                        IsMapping = tableSchema?.IsMapping != null ? tableSchema.IsMapping : false
                    };
                    var childSchemas = RemoveJsonSchemaFromOriginalSchema(tableSchema.ColumnSchemaInfoCollection);
                    foreach (var s in childSchemas)
                    {
                        schema.ChildSchemas.Add(s);
                    }
                    selectedJsonSchemas.Add(schema);
                });
                return selectedJsonSchemas;
            }
            private static ObservableCollection<JsonSchemaInfo> RemoveJsonSchemaFromOriginalSchema(List<TableSchemaInfo> selectedTableSchema)
            {
                ObservableCollection<JsonSchemaInfo> selectedJsonSchemas = new ObservableCollection<JsonSchemaInfo>();
                if (selectedTableSchema == null)
                {
                    throw new ArgumentNullException();
                }
                foreach (var tableSchema in selectedTableSchema)
                {
                    JsonSchemaInfo schema = new JsonSchemaInfo()
                    {
                        SchemaName = tableSchema.ColumnName,
                        FiniteArraySchemaType = tableSchema.FiniteArraySchemaType,
                        InnerArrayCount = tableSchema.InnerArrayCount,
                        IsAnonymousSchema = tableSchema.IsAnonymousSchema,
                        SchemaType = tableSchema.SchemaType,
                        ValueType = tableSchema.ValueType,
                    };
                    foreach (JsonSchemaInfo item in RemoveJsonSchemaFromOriginalSchema(tableSchema.ColumnSchemaInfoCollection))
                    {
                        schema.ChildSchemas.Add(item);
                    }
                    selectedJsonSchemas.Add(schema);
                }
                return selectedJsonSchemas;
            }
            internal static bool GetMappingStatus(List<TableSchemaInfo> SelectedTableSchema, WebConnectionType webConnectionType)
            {
                if (webConnectionType != WebConnectionType.GeneralJson)
                {
                    return false;
                }
                if (SelectedTableSchema != null && SelectedTableSchema?.Count > 0)
                {
                    return false;
                }
                return false;
            }
            public static T Deserialize<T>(string jsonstr)
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonstr);
            }
            internal static string SerializeObject(object value)
            {
                return Newtonsoft.Json.JsonConvert.SerializeObject(value, new Newtonsoft.Json.JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver() });
            }
        }

See also