This article explains how to establish the SQL connection and bind the retrieving data from database in a step by step process: Step 1: Retrieve the data table from the SQL DataSet using the connection string. public class ViewModel { public ViewModel() { try { SqlConnection thisConnection = new SqlConnection(ConnectionString); thisConnection.Open(); string Get_Data = "SELECT * FROM ChartData"; SqlCommand cmd = thisConnection.CreateCommand(); cmd.CommandText = Get_Data; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); var table = ds.Tables[0]; this.DataTable = table; } catch { return; } } public object DataTable { get; set; } public static string ConnectionString { get { string currentDir = System.Environment.CurrentDirectory; currentDir = currentDir.Substring(0, currentDir.Length - 10) + "\\LocalDataBase"; return @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + currentDir + @"\SeriesItemsSource.mdf;Integrated Security=True"; } } } Step 2: In the main page, initialize the SfChart control and bind the retrieved data. <Grid> <Grid.DataContext> <local:ViewModel></local:ViewModel> </Grid.DataContext> <chart:SfChart Margin="10"> <chart:SfChart.PrimaryAxis> <chart:NumericalAxis RangePadding="Additional"/> </chart:SfChart.PrimaryAxis> <chart:SfChart.SecondaryAxis> <chart:NumericalAxis RangePadding="Additional"/> </chart:SfChart.SecondaryAxis> <chart:ScatterSeries ItemsSource="{Binding DataTable}" XBindingPath="xVal" YBindingPath="yVal"/> </chart:SfChart> </Grid> You can download the complete sample in this GitHub location. Output See also How to bind the SQL Database to WPF Charts
This article explains how to establish the SQL connection and bind the retrieving data from database in a step by step process. Step 1: Retrieve the data table from the SQL DataSet using the connection string. public class ViewModel { public ViewModel() { try { SqlConnection thisConnection = new SqlConnection(ConnectionString); thisConnection.Open(); string Get_Data = "SELECT * FROM ChartData"; SqlCommand cmd = thisConnection.CreateCommand(); cmd.CommandText = Get_Data; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); var table = ds.Tables[0]; this.DataTable = table; } catch { MessageBox.Show("DataBase Error"); } } public object DataTable { get; set; } public static string ConnectionString { get { string currentDir = System.Environment.CurrentDirectory; currentDir = currentDir.Substring(0, currentDir.Length - 10) + "\\LocalDataBase"; return @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + currentDir + @"\SeriesItemsSource.mdf;Integrated Security=True"; } } } Step 2: In the main page, initialize the SfChart control and bind the retrieved data. <Grid> <Grid.DataContext> <local:ViewModel></local:ViewModel> </Grid.DataContext> <chart:SfChart Margin="10"> <chart:SfChart.PrimaryAxis> <chart:NumericalAxis RangePadding="Additional"/> </chart:SfChart.PrimaryAxis> <chart:SfChart.SecondaryAxis> <chart:NumericalAxis RangePadding="Additional"/> </chart:SfChart.SecondaryAxis> <chart:ScatterSeries ItemsSource="{Binding DataTable}" XBindingPath="xVal" YBindingPath="yVal"/> </chart:SfChart> </Grid> Download complete sample here
You can assign the data from asynchronous (async) service using Syncfusion ASP.NET MVC ReportViewer. The following steps guide you to assign the data from asynchronous (async) service from the OnReportLoaded method in the Web API controller. 1. Create a simple ReportViewer application with the help of getting started documentation. 2. Get asynchronous (async) service data in the Report API controller from the OnReportLoaded method as shown in the following code snippet. ReportAPIController.cs public class ReportApiController : ApiController,IReportController { public object PostReportAction(Dictionary<string, object> jsonResult) { return ReportHelper.ProcessReport(jsonResult, this); } //Get action for getting resources from the report [System.Web.Http.ActionName("GetResource")] [AcceptVerbs("GET")] public object GetResource(string key, string resourcetype, bool isPrint) { return ReportHelper.GetResource(key, resourcetype, isPrint); } //Method will be called when initialize the report options before start processing the report public void OnInitReportOptions(ReportViewerOptions reportOption) { } //Method will be called when reported is loaded public async void OnReportLoaded(ReportViewerOptions reportOption) { if (reportOption.ReportModel.ProcessingMode == ProcessingMode.Local) { reportOption.ReportModel.DataSources.Clear(); var ReportData = await GetListAsyncData(); reportOption.ReportModel.DataSources.Add(new ReportDataSource { Name = "StoreSales", Value = ReportData }); } } private async Task<IEnumerable<StoreSales>> GetListAsyncData() { List<StoreSales> sales = new List<StoreSales>(); StoreSales strSal = null; strSal = new StoreSales() { SalesOrderID = 43659, TotalDue = 23153.2339, CustomerID = 29825, StateProvinceCode = "GA", Store = "Better Bike Shop" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43663, TotalDue = 472.3108, CustomerID = 29565, StateProvinceCode = "CA", Store = "World Bike Discount Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43673, TotalDue = 4216.0258, CustomerID = 29844, StateProvinceCode = "NH", Store = "Seventh Bike Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43675, TotalDue = 6434.0848, CustomerID = 29827, StateProvinceCode = "MO", Store = "First Bike Store" }; sales.Add(strSal); return sales; } } public class StoreSales { public int SalesOrderID { get; set; } public double TotalDue { get; set; } public int CustomerID { get; set; } public string Store { get; set; } public string StateProvinceCode { get; set; } }
You can assign the data from asynchronous (async) service using Syncfusion ASP.NET ReportViewer. The following steps guide you to assign the data from asynchronous (async) service from the OnReportLoaded method in the Web API controller. 1. Create a simple ReportViewer application with the help of getting started documentation. 2. Get asynchronous (async) service data in the Report API controller from the OnReportLoaded method as shown in the following code snippet. ReportAPIController.cs public class ReportApiController : ApiController,IReportController { public object PostReportAction(Dictionary<string, object> jsonResult) { return ReportHelper.ProcessReport(jsonResult, this); } //Get action for getting resources from the report [System.Web.Http.ActionName("GetResource")] [AcceptVerbs("GET")] public object GetResource(string key, string resourcetype, bool isPrint) { return ReportHelper.GetResource(key, resourcetype, isPrint); } //Method will be called when initialize the report options before start processing the report public void OnInitReportOptions(ReportViewerOptions reportOption) { } //Method will be called when reported is loaded public async void OnReportLoaded(ReportViewerOptions reportOption) { if (reportOption.ReportModel.ProcessingMode == ProcessingMode.Local) { reportOption.ReportModel.DataSources.Clear(); var ReportData = await GetListAsyncData(); reportOption.ReportModel.DataSources.Add(new ReportDataSource { Name = "StoreSales", Value = ReportData }); } } private async Task<IEnumerable<StoreSales>> GetListAsyncData() { List<StoreSales> sales = new List<StoreSales>(); StoreSales strSal = null; strSal = new StoreSales() { SalesOrderID = 43659, TotalDue = 23153.2339, CustomerID = 29825, StateProvinceCode = "GA", Store = "Better Bike Shop" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43663, TotalDue = 472.3108, CustomerID = 29565, StateProvinceCode = "CA", Store = "World Bike Discount Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43673, TotalDue = 4216.0258, CustomerID = 29844, StateProvinceCode = "NH", Store = "Seventh Bike Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43675, TotalDue = 6434.0848, CustomerID = 29827, StateProvinceCode = "MO", Store = "First Bike Store" }; sales.Add(strSal); return sales; } } public class StoreSales { public int SalesOrderID { get; set; } public double TotalDue { get; set; } public int CustomerID { get; set; } public string Store { get; set; } public string StateProvinceCode { get; set; } } Note:A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!
You can assign the data from asynchronous (async) service using Syncfusion ASP.NET Core ReportViewer. The following steps guide you to assign the data from asynchronous (async) service from the OnReportLoaded method in the Web API controller. 1. Create a simple ReportViewer application with the help of getting started documentation. 2. Get asynchronous (async) service data in the Web API controller from the OnReportLoaded method as shown in the following code snippet. HomeController.cs public partial class HomeController : Controller, IReportController { private IMemoryCache _cache; private IHostingEnvironment _hostingEnvironment; public HomeController(IMemoryCache memoryCache, IHostingEnvironment hostingEnvironment) { _cache = memoryCache; _hostingEnvironment = hostingEnvironment; } public ActionResult Index() { return View(); } public IActionResult Error() { return View(); } [HttpPost] public object PostReportAction([FromBody] Dictionary<string, object> jsonResult) { if (jsonResult.ContainsKey("CustomData")) { var paramValue = jsonResult["CustomData"]; } return ReportHelper.ProcessReport(jsonResult, this, this._cache); } [ActionName("GetResource")] [AcceptVerbs("GET")] public object GetResource(ReportResource resource) { return ReportHelper.GetResource(resource, this, _cache); } [HttpPost] public object PostFormReportAction() { return ReportHelper.ProcessReport(null, this, this._cache); } public void OnInitReportOptions(ReportViewerOptions reportOption) { string basePath = _hostingEnvironment.WebRootPath; FileStream inputStream = new FileStream(basePath + @"\ReportData\Region.rdlc", FileMode.Open, FileAccess.Read); reportOption.ReportModel.Stream = inputStream; } public async void OnReportLoaded(ReportViewerOptions reportOption) { if (reportOption.ReportModel.ProcessingMode == ProcessingMode.Local) { reportOption.ReportModel.DataSources.Clear(); var ReportData = await GetData(); reportOption.ReportModel.DataSources.Add(new ReportDataSource { Name = "StoreSales", Value = ReportData }); } } private async Task<List<StoreSales>> GetData() { List<StoreSales> sales = new List<StoreSales>(); StoreSales strSal = null; strSal = new StoreSales() { SalesOrderID = 43659, TotalDue = 23153.2339, CustomerID = 29825, StateProvinceCode = "GA", Store = "Better Bike Shop" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43663, TotalDue = 472.3108, CustomerID = 29565, StateProvinceCode = "CA", Store = "World Bike Discount Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43673, TotalDue = 4216.0258, CustomerID = 29844, StateProvinceCode = "NH", Store = "Seventh Bike Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43675, TotalDue = 6434.0848, CustomerID = 29827, StateProvinceCode = "MO", Store = "First Bike Store" }; sales.Add(strSal); return await Task.Run(() => sales); } } public class StoreSales { public int SalesOrderID { get; set; } public double TotalDue { get; set; } public int CustomerID { get; set; } public string Store { get; set; } public string StateProvinceCode { get; set; } } Please download the sample for asynchronous data here
You can assign the data from asynchronous (async) service using Syncfusion JavaScript ReportViewer. The following steps guide you to assign the data from asynchronous (async) service from the OnReportLoaded method in the Web API controller. 1. Create a simple ReportViewer application with the help of getting started documentation. 2. Get asynchronous (async) service data in the Report API controller from the OnReportLoaded method as shown in the following code snippet. ReportAPIController.cs public class ReportApiController : ApiController,IReportController { public object PostReportAction(Dictionary<string, object> jsonResult) { return ReportHelper.ProcessReport(jsonResult, this); } //Get action for getting resources from the report [System.Web.Http.ActionName("GetResource")] [AcceptVerbs("GET")] public object GetResource(string key, string resourcetype, bool isPrint) { return ReportHelper.GetResource(key, resourcetype, isPrint); } //Method will be called when initialize the report options before start processing the report public void OnInitReportOptions(ReportViewerOptions reportOption) { } //Method will be called when reported is loaded public async void OnReportLoaded(ReportViewerOptions reportOption) { if (reportOption.ReportModel.ProcessingMode == ProcessingMode.Local) { reportOption.ReportModel.DataSources.Clear(); var ReportData = await GetListAsyncData(); reportOption.ReportModel.DataSources.Add(new ReportDataSource { Name = "StoreSales", Value = ReportData }); } } private async Task<IEnumerable<StoreSales>> GetListAsyncData() { List<StoreSales> sales = new List<StoreSales>(); StoreSales strSal = null; strSal = new StoreSales() { SalesOrderID = 43659, TotalDue = 23153.2339, CustomerID = 29825, StateProvinceCode = "GA", Store = "Better Bike Shop" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43663, TotalDue = 472.3108, CustomerID = 29565, StateProvinceCode = "CA", Store = "World Bike Discount Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43673, TotalDue = 4216.0258, CustomerID = 29844, StateProvinceCode = "NH", Store = "Seventh Bike Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43675, TotalDue = 6434.0848, CustomerID = 29827, StateProvinceCode = "MO", Store = "First Bike Store" }; sales.Add(strSal); return sales; } } public class StoreSales { public int SalesOrderID { get; set; } public double TotalDue { get; set; } public int CustomerID { get; set; } public string Store { get; set; } public string StateProvinceCode { get; set; } } Please download asynchronous data sample here
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a PDF table in document from DataTable in C# and VB.NET. Steps to create table in PDF from DataTable programmatically: Create a new console C# application project. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework applications from NuGet.org. Include the following namespaces in the Program.cs file. C# using Syncfusion.Pdf; using Syncfusion.Pdf.Grid; using System.Data; using System.Drawing; VB.NET Imports System.Drawing Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Grid Use the following code snippet to create table in a PDF file from DataTable. C# //Create a new PDF document using (PdfDocument doc = new PdfDocument()) { //Add a page PdfPage page = doc.Pages.Add(); //Create a PdfGrid PdfGrid pdfGrid = new PdfGrid(); //Create a DataTable DataTable dataTable = new DataTable(); //Add columns to the DataTable dataTable.Columns.Add("ID"); dataTable.Columns.Add("Name"); //Add rows to the DataTable dataTable.Rows.Add(new object[] { "E01", "Clay" }); dataTable.Rows.Add(new object[] { "E02", "Thomas" }); dataTable.Rows.Add(new object[] { "E03", "George" }); dataTable.Rows.Add(new object[] { "E04", "Stefan" }); dataTable.Rows.Add(new object[] { "E05", "Mathew" }); //Assign data source pdfGrid.DataSource = dataTable; //Draw grid to the page of PDF document pdfGrid.Draw(page, new PointF(10, 10)); //Save the document doc.Save("Output.pdf"); } VB.NET 'Create a new PDF document Using doc As PdfDocument = New PdfDocument() 'Add a page Dim page As PdfPage = doc.Pages.Add() 'Create a PdfGri Dim pdfGrid As PdfGrid = New PdfGrid 'Create a DataTable. Dim dataTable As DataTable = New DataTable 'Add columns to the DataTable dataTable.Columns.Add("ID") dataTable.Columns.Add("Name") 'Add rows to the DataTable dataTable.Rows.Add(New Object() {"E01", "Clay"}) dataTable.Rows.Add(New Object() {"E02", "Thomas"}) dataTable.Rows.Add(New Object() {"E03", "George"}) dataTable.Rows.Add(New Object() {"E04", "Stefan"}) dataTable.Rows.Add(New Object() {"E05", "Mathew"}) 'Assign data source pdfGrid.DataSource = dataTable 'Draw grid to the page of PDF document pdfGrid.Draw(page, New PointF(10, 10)) 'Save the document doc.Save("Output.pdf") End Using A complete working sample can be downloaded from Create-PDF-Table using DataTable.zip By executing the program, you will get the PDF document as follows. Take a moment to peruse the documentation, where you can find other options like drawing right-to-left text and multi-column text, consuming TrueType fonts, Standards fonts, and CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, and protect PDF documents with code examples. Refer here to explore the rich set of Syncfusion Essential® PDF features. An online sample link to create table in a PDF file. Note:Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message. Conclusion I hope you enjoyed learning about how to create table in a PDF file from DataTable. You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data. For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our WinForms Diagram and other WinForms components. If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!