![]() ![]() Crystal Reports Using a Data. Set Data. Table. Crystal Reports Using a Data. In this article, I would like to explain CRUD operations in Windows applications using C#. Crystal Reports - Dynamic WebForms Using a DataSet and SQL DataReader In Custom Class. This article describes how to retrieve data dynamically into a DataSet DataTable. There are all sorts of ways to get the Last Inserted ID but the easiest way I have found is by simply retrieving it from the TableAdapter in the DataSet like so. Often, the best way to prevent an error is to understand why it occurs. In various newsgroups, questions regarding the Crystal Reports LogOnException are asked quite. Set Data. Table. Crystal Reports - Dynamic Web. Forms Using a Data. Set and SQL Data. Reader In Custom Class. This article describes how to retrieve data dynamically into a Data. Set Data. Table. The Data. Table (not the Table. ![]() Adapter Object) in the Data. Set does not have a connection to the database. The Data. Table columns represent the data fields that will appear on the Crystal Report. The Data. Set will be populated with data from the Adventure. Works database using a SQLData. Reader created in a Class. Create the SQL Server Data Source for Crystal Reports. This project will use a SQL stored procedure called Sales. Sales. Customers from the Adventure Works database. The View in the Stored Procedure code calls a custom view called dbo. Sales. Customers. PROCEDURE [Sales].[usp_Sales. Customers]ASSELECT Customer. ID, Customer. Name, Account. Number, Territory, Total. YTD from [dbo].[vw_Sales. Customers]. Code for the View. ALTER VIEW [dbo].[vw_Sales. Customers]ASselect Sales. Individual. Customer. ID,First. Name + ' ' + Last. Name as Customer. Name, Customer. Account. Number. Sales. Territory. Name as Territory,Demo. Individual. Survey"; (dr: Total. Purchase. YTD)[1]','varchar(5. Total. YTDfrom. Person. Address INNER JOIN. Sales. Customer. Address ON Person. Address. Address. ID = Sales. Customer. Address. Address. IDINNER JOIN. Sales. Customer ON Sales. Customer. Address. Customer. ID = Sales. Customer. Customer. ID AND. Sales. Customer. Address. Customer. ID = Sales. Customer. Customer. ID INNER JOIN. Sales. Individual ON Sales. Customer. Customer. ID = Sales. Individual. Customer. ID INNER JOIN. Person. Contact ON Sales. Individual. Contact. ID = Person. Contact. Contact. IDINNER JOIN. Sales. Sales. Territory ON Sales. Customer. Territory. ID = Sales. Sales. Territory. Territory. ID AND Sales. Customer. Territory. ID = Sales. Sales. Territory. Territory. ID. outer apply. Demographics. nodes('declare namespace dr="http: //schemas. Individual. Survey". Individual. Survey') as Demo(ref). Create an ASP . NET Web. Site in Visual Studio. Adding the Data. Set and SQL Server Connection Class. Right click in the Solution Explorer on the project and click Add New Item. Select Data. Set from the templates listed and click Add. Add a Data. Table to the empty Data. Set. Change the Name of the Data. Table by clicking in the top portion. Right click on the Data. Table to Add the Columns (field names) to the Data. Table. Add a class to the Project. This will be the class that will return a populated Data. Set to the Default. Web. Page to be used as the Report. Data. Source for a Crystal. Report. Add using statements to include SQL classes and objects in the new class. System. Data. Sql. Client; using System. Data. Add the code to the class that will create a Sql. Connection object and Sql. Command object to connect to the database and execute the stored procedure into a Sql. Data. Reader. The code will create a new Data. Set with the same schema as the Data. Set. 1 created earlier and then load the Data. Set's Data. Table with the data from the Sql. Data. Reader. Finally, it will return the populated Data. Set to the Default webpage. Important: Add the namespace attribute above the class name. It will be used in the webpage to access the method created to return the Data. Set. namespace Report. Test{public static Data. Set sql. Report. Data(){ try{string scmd = "Sales. Sales. Customers"; string scon = @"Data Source=Your. SQLServer; Initial Catalog=Adventure. Works; Integrated Security=SSPI; "; Sql. Connection cn = new Sql. Connection(scon); cn. Open(); Sql. Command cmd = new Sql. Command(scmd, cn). Command. Type = Command. Type. Stored. Procedure; Sql. Data. Reader dr = cmd. Execute. Reader(Command. Behavior. Close. Connection); Data. Set ds = new Data. Set(); ds = new Data. Set. 1(); ds. Tables["Sales. Customers"]. Load(dr); return ds; }catch (Exception ex){throw ex; }}}. Adding the Crystal Report. Create the Crystal. Report by clicking Add New Item. The wizard will appear and by clicking Using the Report Wizard, you can step through to select the Data. Set and the fields for the report. Select the Data. Set. Selected Tables box. Click on the > > button to add all of the fields to the report. Select the Style of the report and click Finish. Open the Field. Explorer to Expand the Data. Set and drag and drop fields onto the report and format them as needed. Adding the Crystal Report to the ASP . Net Web Page. Double click the Default. Solution Explorer to open it. Select Split or Design View to get the Toolbox to appear. Drag and drop a Crystal. Report. Viewer onto the Design portion of the page. In the Solution Explorer, double click the Default. Add these lines of code to the top of the page under the Using statements. Report. Test; using Crystal. Decisions. Crystal. Reports. Engine; using Crystal. Decisions. Report. Source; using Crystal. Decisions. Web; using System. Data. Common; Add this code within the page class. This code will create a new Data. Set and populate it with the data from our custom class by calling the static method sqlreport. Data(). Using the Report. Document object, the data source for the report can be set and then loaded dynamically to the Viewer at run time. Report. Document rd = new Report. Document(); protected void Crystal. Reports. Load(){try{Data. Set ds = new Data. Set(); ds = SQL_Class. Report. Data(); report. Path = Server. Map. Path("Crystal. Report. Sales. rpt"); rd. Load(report. Path); rd. Set. Data. Source(ds); rd. Print. Options. Paper. Orientation = Crystal. Decisions. Shared. Paper. Orientation. Landscape; Crystal. Report. Viewer. 1. Report. Source = rd; Crystal. Report. Viewer. 1. Data. Bind(); }catch (Exception ex){throw ex; }}Finally, add code to detect the Page_Init event and call the code to run & databind the report here. Page_Init(object sender, Event. Args e){try{Crystal. Reports. Load(); }catch (Exception ex){ex. Message. To. String(); }}. Run the program to verify that the report runs without any problems or errors.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |