I've heard that the best way to print a DataTable is to use ReportViewer. However I found it more difficult to achieve this than I thought. I am not familiar with ReportViewer, so perhaps I want to find a solution that is non-existent.
"reportView" is the ReportViewer control.
//I am creating a fresh new DataTable here.
DataTable reportDT = new DataTable();
reportDT.TableName = "reportDT";
DataColumn dataColID = reportDT.Columns.Add("ID");
DataColumn dataColValue = reportDT.Columns.Add("Value");
DataRow newrow = reportDT.NewRow();
newrow["ID"] = string1;
newrow["Value"] = string2;
reportDT.Rows.Add(newrow);
//Until now, this works perfectly with DataGridView.
//I am creating a new source object, hopefully from my datatable.
ReportDataSource source = new ReportDataSource("reportDT", reportDT);
reportView.ProcessingMode = ProcessingMode.Local;
reportView.LocalReport.DataSources.Add(source);
this.reportView.RefreshReport();
Running the app like this throws no exception, but it shows no data in the reportview either. It says: "The source of the report definition has not been specified".
I have realized that it might need a .rdlc file, which must be created design time. But if I create it design time, it wants me to fill it with data, which I cannot do, because the data is created runtime. So I want to display a bunch of data in ReportViewer, but the above code does not work. What is wrong with it?
You need to create a report file. The reportViewer1 is a Control, not a Report and therefore cannot display data directly.
To avoid repeating code, your answer can be found here.
Related
i try to custom on existing report to add some field name from Table ASMBTL (for example) into field list on existing report which have create and compile.
I know have to create an relation, i try different approach still fail due to i still new to C# and devexpress programming.
The Attachment of the image is what i try to learn and success by using C# coding and can extract any value from any table which not include inside the filed list of any original report. I try to learn this way so i would able to custom any existing report just using c# script anytime at anyplace.
Below is some code i learn from web and manage to add the tab "Assembly Custome Add On Table" inside the field list, but is not success to put it in under "Stock Assembly Master". I still can't understand how to add relation to proceed like the photo attachment result.
DataTable dtMyTable = GetCustomTableData();
dtMyTable.TableName = "Assembly Custome Add On Table";
AddCustomTable(ds, dtMyTable);
private void AddCustomTable(DataSet ds, DataTable dt) {
if (ds.Tables.Contains(dt.TableName) == false)
ds.Tables.Add(dt); }
private DataTable GetCustomTableData()
{Return __report.DBSetting.GetDataTable("Select DtlKey, DocKey, OrderQty" from ASMDTL", false);
This is what I try to accomplish
I have found the solution of my problem base on How to make relation between tables which are in same dataset? link had shown me the way to solution.
I'm attempting to access a basic table from a local database in my Windows Forms project. I seem to have created the database correctly, and imported the dataset as expected as it displays in my Solution Explorer.
However, I am stuck on how to actually access the data within my database. I've attempted many different solutions but I cannot seem to get anywhere.
Here is what I've accomplished so far:
Solution Explorer Screenshot
But I cannot figure out how to make queries to the dataset, whether it's selecting, updating, or deleting rows.
The closest I've come to getting the data is from the code here:
InventoryDatabaseDataSet context = new InventoryDatabaseDataSet();
var inv = context.Tables["Inventory"];
var all = inv.Select();
But it doesn't return any seemingly valid data.
How do I go about making queries to my dataset? I understand Linq is the common method of making queries, but I don't understand how to get to the point of being able to do such a thing.
Any help would be greatly appreciated, thanks!
The DataSet item in the Solution Explorer represents a number of types. Just as String, Int32 and Form are types, so InventoryDatabaseDataSet is a type. Just like other types, you create an instance in code and then use it.
You will also find that those types that are components have been added to your Toolbox and can be added to forms in the designer, just like other controls and components.
You can also drag items from the Data Sources window and have the designer generate appropriate objects and code. For instance, if you drag a table from the Data Sources window to a form, it will generate a DataSet to store the data, a table adapter to retrieve data from the database and save changes back, a DataGridView to display the data, a BindingSource to link between the DataTable and the DataGridView and a BindingNavigator to navigate the data.
If you do use the designer then you'll see code generated to retrieve data by calling Fill on the table adapter and save changes by calling Update. If you want to do it in code yourself then you can do it something like this to retrieve:
var data = new InventoryDatabaseDataSet();
using (var adapter = new InventoryTableAdapter())
{
adapter.Fill(data);
}
this.InventoryBindingSource.DataSource = data.Inventory;
this.InventoryDataGridView.DataSource = this.InventoryBindingSource;
and this to save:
this.InventoryBindingSource.EndEdit();
var data = (InventoryDataTable) this.InventoryBindingSource.DataSource;
using adapter = new InventoryTableAdapter())
{
adapter.Update(data);
}
After storing data in DataSet,DataSet is read through DataTable object.
Similarly object of DataRow is used to read the row of a table.
Following is the sample code
InventoryDatabaseDataSet ds = new new InventoryDatabaseDataSet();;
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand(#"SELECT * FROM FooTable", connString);
da.Fill(ds, "FooTable");
dt = ds.Tables["FooTable"];
foreach (DataRow dr in dt.Rows)
{
MessageBox.Show(dr["Column1"].ToString());
}
This is my first time working with DataSets and BindingSources, so please be gentle on me.
As part of a more complicated reporting system (I've distilled it down to a basic incarnation, but it still won't run correctly), I'm trying to pull data from a database using a DataSet problematically (that is, not set up via the designer). Here is the code I have so far:
// pull the data set
var dsReportData = new Reports2.ReportTest2();
dsReportData.BeginInit();
dsReportData.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
// bind tha data set
BindingSource bsReportBinding = new BindingSource();
((ISupportInitialize)bsReportBinding).BeginInit();
bsReportBinding.DataMember = dsReportData.Tables[0].TableName;
bsReportBinding.DataSource = dsReportData;
bsReportBinding.ResetBindings(true);
// test this stuff
dgvTester.DataSource = bsReportBinding;
dsReportData.EndInit();
((ISupportInitialize)bsReportBinding).EndInit();
I based this on the code I saw in a .designer.cs file after setting up binding through the designer. dgvTester is just a DataGridView with the default properties created in the designer.
The ReportTest2 dataset has just one TableAdapter in it, added via designer.
Now, if I went to Data -> Preview Data in VS and previewed the ReportTest2.payments.Fill,GetData () object, it returns data just fine, same as if I ran the query I used to crate the TableAdapter in SQL Server Management Studio.
However, running the actual code results in the DataGridView getting the column names from the query result, but not the actual data. The debugger reveals that dsReportData.payments.Rows.Count == 0 (and that, yes, dsReportData.Tables[0] is payments).
I ultimately intend to use the BindingSource to provide data to a ReportViewer, but first things first is making sure there's no problems with retrieving the data before going onto debug the report.
Hopefully I'm missing something obvious here. I hope so...
Figured it out after some trial and error. This is the part I was missing:
var TableAdapter = new Reports2.ReportTest2TableAdapters.paymentsTableAdapter();
TableAdapter.Fill(dsReportData.payments);
I didn't see it in the code I was referencing because the designer snuck it into the .cs file instead of the .designer.cs file. This made the data appear.
I have filled datatable. I want to display it in the report. I'm doing like this:
ReportDataSource source = new ReportDataSource("dtss", dt);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(source);
ReportViewer1.DataBind();
ReportViewer1.LocalReport.Refresh();
But when i try to build, i get error:
The dataset ‘DataSet1’ refers to the data source “”, which does not exist.
What am I missing? Using Webforms(ASP.net)
The name that you give your ReportDataSource needs to match the name of the DataSet (defined within the report). For example, on line 1 (of your code, above) you need to say:
ReportDataSource source = new ReportDataSource("DataSet1", dt);
or rename the dataset (in your report) to "dtss"
Alternative.
Apparently if you add a DataSet within the rdlc file it auto associates the name in the xml, and when you delete that from the rdlc it doesn’t remove it from the xml, so it just gives you a reference which it thinks of as an empty dataset, and cannot evaluate. I first named my second one DataSet2 and then after setting it up in the design decided to delete it and rename it, but you have to go back to the xml and delete the entire empty DataSet node<>.
Hope this helps someone who's problems cannot be solved by the above answer.
Is that possible to bind two dataset at a same time in asp.net crystal report?
I tried the code below, but it asks for server details:
Invoice inv = new Invoice();
inv.OrgId = Session["org_id"].ToString();
inv.InvoiceId = invoiceId.ToString();
ds = _reportController.ReportPrintBillView(inv);
dtBill=ds.Tables[0];
dtInvoice = ds.Tables[1];
ReportDocument myRpt = new ReportDocument();
myRpt.Load(Server.MapPath("PrintandprintBill.rpt"));
myRpt.SetDatabaseLogon("root", "root", "localhost", "hemaepdb");
myRpt.SetDataSource(dtBill);
myRpt.SetDataSource(dtInvoice);
CrystalReportViewerPrint.ReportSource = myRpt;
CrystalReportViewerPrint.Visible = true;
No, the report accepts one datasource. However your subreports may have a different datasource.
EDIT:
If you need data from two different datasets that have similar data you might try to combine the data into one dataset. Even if some of the data duplicates, you can then create groups and use the suppress functionality to only show and format the data in the way you need to see it. See my answer here for a better explanation of the grouping and suppressing that I am referring to.
From ur Requirement i think u want to use View. View is one kind of logical table that maps other columns from different tables. And at Datable just use View Name and add this datatable to dataset. simply SetDataSource to dataset.