Articles in this section
Category / Section

Change the WEB API Connection String Dynamically

Published:
Updated:

In Bold Reports, you can dynamically change the web API data source connection details 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 changing 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 is stored in JSONResult, and you can store it 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. Utilize the jsonResult information with the ReportHelper.GetDatasource API to retrieve the data source details of the reports. To change the connection string of the data source, use the DataSourceCredentials object.

            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 provides two methods: Deserialize and SerializeObject. These methods enable you to deserialize a JSON string into an object and serialize an object into a JSON string, 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() });
            }
        }
    

By following these steps, you can dynamically change the connection string of the WEB API data source in Bold Reports. This feature allows you to switch between different data sources, update connection strings, and adjust other connection details as needed, enhancing your reporting capabilities.

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied