Search results
Suggest a FeaturePDF

How to change the WEB API connection string dynamically

Bold Reports® can change the web API data source connection details dynamically at runtime.

This means that users can modify the connection details of their web API data source without having to restart their application or perform any additional configuration steps. This feature allows greater flexibility in managing data sources and simplifies the process of connecting to various data sources.

By being able to change the web API data source connection details dynamically at runtime, users can easily switch between different data sources, update connection strings, and adjust other connection details as needed. This enhances the reporting capabilities of Bold Reports® and allows users to quickly and easily adapt to changing data requirements.

Find the following steps to change the WEB API connection string:

  1. The data source information are stored in jsonResult, and you can store them in the 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 and you have to use the 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 consists of both Deserialize and SerializeObject methods to return the deserialized object from JSON string and to return the JSON string representation of the object respectively.

            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() });
            }
        }
Having trouble getting help?
Contact Support
Having trouble getting help?
Contact Support