Crystal Report multi table failure - c#

I'm still new to C# and reports, and in order to take baby steps, I started with a Crystal Report using one table.
Eventually I figured it out and it worked brilliantly.
Then I added another table to the report. I haven't changed anything in my code. Adding a field from the second table to the report, results in a blank report.
Removing that field again (so no columns form the second table is on the report), the report produces data again.
So I get the impression that the problem is on the report side. But I have included the code anyway:
private void Load_Ord_Rep()
{
using (MySqlConnection conn = new MySqlConnection(OTW.Properties.Settings.Default.wcdbConnectionString))
{
conn.Open();
String sql = "SELECT * FROM wcdb.order_table, wcdb.mat_table WHERE order_no = '13661' and order_table.mat_code = mat_table.mat_code";
using (MySqlCommand cmdSel = new MySqlCommand(sql, conn))
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
da.Fill(ds);
ReportDocument rpt = new ReportDocument();
rpt.Load("C:\\Visual Studio 2008\\Projects\\OTW\\OTW\\CrystalReport3.rpt");
dataView1.Table = ds.Tables[0];
rpt.SetDataSource(dataView1);
crystalReportViewer1.ReportSource = rpt;
crystalReportViewer1.Refresh();
}
conn.Close();
}
}
With further investigation I have come to the conclusion the problem is not the code or the link, but rather the loading of the second table. I did a outer join with the values being equal or greater. Only the first table's results are displayed on the report. So because the second table's values are not read, no join can be established between the two tables and thus no data on the report. Now the question: why is the second table not being read by Crystal Report!?
UPDATE
I removed the second table from the main report and added a sub-report with the data. Same result as before. The sup report shows blank. Running the sup report on its own (as the main report), it populated correctly. I'm using MySQL, could it then maybe be a database issue?
UPDATE
I created a new app, this time connected the report to the database using ODBC (instead of ADO.NET). And it worked perrrfect. Now to figure out why ADO.Net is not working....as my entire program is based on it.

You don't need to do any of the code you are doing with the dataset/dataview. I recommend you allow the report to "pull" data (you are "pushing" data to the report now).
To pull data requires only the following:
ReportDocument rpt = new ReportDocument();
rpt.Load("C:\\Visual Studio 2008\\Projects\\OTW\\OTW\\CrystalReport3.rpt");
rpt.SetDataBaseLogon("userName", "password", "servername", "database");
crystalReportViewer1.ReportSource = rpt;
This reduces the risk that you're confusing the report up by passing in a dataview that it has no idea how to map to the tables you added while designing the report. (Which I'm 99.999% sure is happening now.)
All the code above lacks is parameters if you had any that needed to be set. It looks like you may be trying to "push" data because you wanted to filter on that order number or something? If that is the case, add a 'record selection formula' inside your report, base it's 'where' aspect of that formula on a Crystal Report parameter and add a rpt.SetParameter(arguments) line to the code I provided.
No this is not a licensing constraint, I'm certain of that.
Successful troubleshooting path:
1) Used the simple code to load the report:
* No errors, but no data in the report
2) Played with join options in the report:
* Also resulted in a blank report, even with only one table
* Concerned the second table is not reading data
3) Checked for record filtering by a 'record selection formula' which might limit/prevent rows from being returned
* In this case, wasn't applicable
4) Analyzed how joins were being done:
["It's probably how you are joining. For example: if you do a right outer join to a second table and it has no data, then even the first table's rows won't come back."]
A join was done on the primary id field (mat_code) in the 'orders' table to the same field (mat_code) in the second table, 'material'. This seemed fine.
Attempted a Left Outer join with the link type being ">=". The report printed the first table (orders) with data. But still none of the second tables' (material) data.
Made a new report, this time first adding the "second" table (material) and then the first (orders). This time with an outer join, and only the material table shows data. In other words, it only appears to pull data for the first, or primary table, then stops.
5) Attempted loading the second table's data by putting it into a subreport, and linking the subreport on the same 'mat_code' field:
(Helpful tutorial: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_subreport.htm)
Sub-report also shows blank. Only the main report works. Whatever table exists in the subreport is not being populated.
Still sees no data using the simple suggested code, his original code still shows the first table's data
6) Check for a mismatch between the table's connection and definition, and the actual data structure/contents being submitted to, or loaded by, the report. A specific test was suggested, go to:
i) 'Set Database Location' (where you manage tables for the report)
ii) Look to see if an XML/DataSet was used versus OLEDB was done
iii) Change the database table location to the same tables, but with an OLEDB connection type (repeat for all tables)
When attempting above, when the 'Update' button was hit, the screen just flickered for a split second, but showed no message. After testing the report again, still no change in behavior. He was using ADO.NET to connect to the data.
7) Still highly suspicious of the data table definition and connection type. Suggested the following test:
i) Make a brand new 1 page application with only simple test code.
ii) Make a new report with only the 'orders' and 'materials' tables, using OLEDB from the beginning
iii) Add only the mat_code field from the main table to the report
iv) Add a subreport for 'materials' linked on mat_code
v) Show only mat_code on the subreport
vi) Run the app
If data shows, the problem is either:
Answer 1: A mismatch between the database definition (as was read when the report was FIRST made, and connected via ADO.NET to the tables) and the actual data table/column definitions being found when loading the report later (i.e. Someone edited the 'materials' table to change a column definition, or the number of fields, etc.)
Answer 2: Possible defect in the particular combination of Crystal Reports and Windows drivers required to push ADO.NET data to the report. Using the pull-data model over OLEDB may be working around something issues. Those issues may be addressed with the latest Crystal Reports and/or Windows drivers (i.e. hotfixes or Windows updates, driver packs, etc.)
The test worked fine. Even without making a subreport. The data pulled in just like it was supposed to.

Check the link between the two tables in crystal reports. Are they as what you expect ?
Check it by
Field Explorer> DataBaseFields > (RightClick) DatabaseExpert > (See Tab) Links

Related

Importing CSV file into Access using C# but data is out of order

Going a bit old school today trying to fix a bit of software that is running in a manufacturing plant importing data from a CSV file into an access database which is used to generate Crystal reports!
The original CSV file is in RANK (1,2,3, etc) order but when its imported using the following code it often arrives in the Access database out of order.
using (OleDbCommand accComm2 = new OleDbCommand(String.Format("SELECT TOP 100 PERCENT vCSV.* INTO {0} FROM [Text;FMT=Delimited;HDR=YES;IMEX=1;DATABASE={1}].[{2}] as vCSV ORDER BY vCSV.RANK", dbtable, Path.GetDirectoryName(fname), Path.GetFileName(fname)), accConn, accTran))
{
accComm2.ExecuteNonQuery();
}
This causes issues further down the line as the quirky crystal report manufacturing summarys that are created require the data to be in rank order.
Because the Crystal report is grouped into batches, you cant do the sorting on the crystal report end.
Does anyone have any ideas why the above wouldnt be working? Weve even tried importing it to a temporary table and then to the live table, effectively doing the sort command twice but it still sometimes fails!
You should sort in a subquery before inserting. Sorting while inserting has no effect in Access, besides the preview you can get while inserting.
SELECT * INTO {0} FROM (SELECT TOP 100 * [Text;FMT=Delimited;HDR=YES;IMEX=1;DATABASE={1}].[{2}] ORDER BY RANK) as vCSV
Note that, strictly speaking, the resulting table has no index thus is a heap and has no defined sort order (there is no first/second/nth record, any ordering is equally valid). Practically, this will likely work, though.

Datagridview Error After Rowfilter -> Data Update/Add and Fill Process

I have a datagridview which filled by SQL table. When I search a value with a textbox in the data and update/insert the some values and use fill function again I get this error
"System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object."
Note: No problem with update, insert and fill function without using search textbox
This is my fill function;
DataTable dt1 = new DataTable();
void fill()
{
try
{
//txtSearch.Clear();
dt1.Rows.Clear();
dt1.Columns.Clear();
SqlDataAdapter da = new SqlDataAdapter("Select * From Bilgisayar_Zimmet", bgl.baglanti());
da.Fill(dt1);
dataGridView1.DataSource = dt1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Here is my search code;
private void txtSearch_TextChanged(object sender, EventArgs e)
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = String.Format("Name LIKE '%{0}%' OR Surname LIKE '%{1}%'", txtSearch.Text, txtSearch.Text);
}
I think my problem is txtSearch_TextChanged methods. It blocks fill function because in the dgv there is filtered row.
So, if I was hanging my hat on the DataTable peg I would do the following in a net framework project - large parts of this process are switched off in netcore+ projects because of bugs/incomplete support in VS. It does work in core as a runtime thing, just not design (so you could do this design in framework, and import the code to core, or even have a core and netfw project that edit the same files..):
I would...
Have my table in my DB:
Make a new DataSet type of file:
Right click the surface and add a tableadapter
Add a connection string, and choose SELECT
Write a query - personally I recommend putting like select * table where primarykeycolumn = #id here rather than selecting all, but you apparently want to have all 2000 rows in the client. Have a look in advanced and check that all 3 boxes are ticked
Give the query a decent name - if youre selecting by#id then call it FillById etc
You're done
Flip to the forms designer for your form, and open the data sources panel (View menu, Other windows). Drag the node representing the table onto the form:
A load of stuff appears at the bottom, a grid appears, the toolbar appears. Add a textbox too (the asterisk) - we'll use it for the filter
Double click the textbox; you'll see the minimal code VS has written to load the data. The rest of the code is in the Form.Designer.cs file and the DataSet.Designer.cs file if you want to have a look. In our form we just need this code:
I'd paste it as text, but genuinely, the only line of it I actually wrote was the one line in the TextChanged:
bilgisayarZimmetBindingSource.Filter = String.Format("FirstName LIKE '%{0}%' OR LastName LIKE '%{0}%'", txtSearch.Text);
you can reuse placeholders in a string format by the way. Note that the filtering is done on the bindingsource - a device that VS has put between the table and the grid. BindingSources are very useful; they maintain position/current-row knowledge and can transmit updates up and down between grid and datatable. When related data is shown they automatically filter child datatables to show only children of a current parent
We can now run the app and load data, save data and filter data, all off that one line of code. I add a bunch of people:
See how their IDs are all negative? The datatable has an autoincrement that steps -1 then the db will calc the values when we hit save and they will be retrieved automatically, patched into the datatable and the grid will update, all automatically. If these rows were the parent of other rows, the child rows ParentId would be updated too via a DataRelation in the dataset:
We haven't had to refill anything; VS just got the single IDs out the DB
I can type a filter:
No errors; these people are editable, saveable, I can clear the textbox and see everyone etc.
So what is this voodoo? How does it work? Well.. It's not much different to yours. If we go rummaging in DataSet.Designer.cs we can find the queries that pull and push data:
read and write queries right there; the tableadapter wraps a dataadapter and sets up the relevant queries, the datarow state drives whether an insert or update etc is fired.. All the code in your question and more is written by VS and tucked away.. The dataset has strongly typed tables in that inherit from DataTable, and rows that have proper properties rather than just an array you access via string column names
We would just load data:
var dt = tableAdapter.GetData();
var dt2 = new BlahBlahDataTable();
tableAdapter.Fill(dt2);
//if we put parameters in the dataset query like SELECT * FROM person WHERE name = #n
var dt3 = new PersonDataTable();
ta.FillByName(dt3, "John");
The strongly typed datatables are nicer to LINQ with
//weakly typed datatable
dt.Rows.Cast<DataRow>()
.Where(r => r.Field<string>("FirstName").Contains("John") || r["LastName"].ToString().Contains("John"))
//strongly typed datatable
dt.Where(r => r.FirstName.Contains("John") || r.LastName.Contains("John"))
etc..
Final point: I personally wouldn't download 2000 rows into the client and hold them there. I'd download what I needed as and when. The longer you have something the more out of date it's likely to be, the more meory it consumes and the more your server disk/network gets battered serving up volumes of data noone uses. I understand the tradeoff between loading all the rows and then burning the client's CPU to search them rather than the server's, and that "string contains" are hard to optimally search/index, but still - I'd look at full text indexing and leaving them on the server
When you're making your query above, don't put SELECT * FROM BilgisayarZimmet - put SELECT * FROM BilgisayarZimmet WHERE ID = #id and call it FillById. Then when you're done with that one, add another query, SELECT * FROM BilgisayarZimmet WHERE FirstName LIKE #n OR LastName LIKE #n and call it FillbyNameLike
In code, call it like this:
tableAdapter.FillByNameLike(theDatasetX.BilgisayarZimmet, "%" + txtSearch.Text + "%")
Or even better let the user supply the wildcard, tohugh people are perhaps used to writing * so:
tableAdapter.FillByNameLike(theDatasetX.BilgisayarZimmet, txtSearch.Text.Replace("*", "%"))
When the TableAdapter fills it will clear the datatable first, so no need to clear it yourself. To turn this behaviour off, set tableAdapter.ClearBeforeFill = false. This way your grid shows only the data downlaoded from the DB, and that's a small, more recent dataset, than querying it all at app start
All in, VS write a lot of tedious code,and makes life a lot nicer. I use these nearly exclusively if operating in DataTable mode

Crystal Reports for Visual Studio: display 2 unrelated tables

I ask for advice what would you suggest to display 2 unrelated tables in a single report (if needed with subreport), the first one with groupings and the second one "flat"?
Details:
I am developing a report with C# 4 and Crystal Reports for Visual studio.
Data comes from a Stored procedure in Sql Server 2005, that returns 2 tables.
I pass the data to the report with the .SetDataSource(Dataset) method and I checked that data in the dataset is correct.
I need to display the data from the first table in a quite complex form with different sections. This part works.
At the end of the report I need to print the data from the second table (no grouping, just a "flat" print). To do this I added a subreport.
The tables are not related. I added a Dummy field on them with the same value and used it to mimic a relation in the database expert; I configured the subreport link, based on the dummy field.
I have 2 problems.
The data in the main report is cross joined with the second table (I could expect that since they are related with a constant field)
The subreport is always empty (I checked several times that its table in datasource contains correct data).
I tried different configurations of link and tried to place the subreport in different sections (It should be in the report footer).
I set a border around the subreport: the border is printed but inside it is empty.
I also setup a formula to count the records of both tables and put the count in the report: the count is > 0 for both.
Your approach looks wrong.... At first if both are not releated I don't see any use in creating a dummy field and linking tables which will always provide cross join. Any way as per your descrition table 1 data is saperately displayed and table 2 data is saperately displayed
As per your description in main report you need data from table 1 and in footer you need data from table 2.
Try this approach:
Don't link tables
take table 1 in main report and show the required display.
Take sub report and in that take only table 2 and display in footer and make sure don't link main report and sub report nor link two tables.
Ideally speaking, without linked table not used in crystal report as reporting tools used to display the relevant data based on our criteria and display 2 different set of data without link does not mean as a report as well as tough to integrate in crystal report.
Tough to integrate means at any point you have to link the table either via union in sqlserver or at crystal report side, other wise it display as Cartesian.
As per my thoughts you can do it by some approach.
Approach 1 : You can get the data by union or unionAll with set of columns and filter the data where table's column does not have null value ie.
http://www.maximumimpactsolutions.co.uk/blog/comments.asp?bd=118
Table1col1 Table1col2 Table1col3 Table2Col1 Table2Col2 Table2Col3
value value value null null null --table1data
null null null value value value --table2data
Approach 2 : Second is sub-report, but still need the linked as you already done. But there is proper rule to be set (this is very very tedious job).
Approach 3 : Generate 2 report at c# side and merge into one PDF, then export it.
check this links.
Reporting on multiple tables independently in Crystal Reports 11
http://www.tek-tips.com/viewthread.cfm?qid=241195
http://www.crystalreportsbook.com/Forum/forum_posts.asp?TID=4756
http://www.codeproject.com/Questions/348959/Crystal-reports-for-multiple-tables

Same Query gives different results from within Sqlite Expert [duplicate]

I'm trying to populate a DataTable, to build a LocalReport, using the following:
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = new MySqlConnection(Properties.Settings.Default.dbConnectionString);
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT ... LEFT JOIN ... WHERE ..."; /* query snipped */
// prepare data
dataTable.Clear();
cn.Open();
// fill datatable
dt.Load(cmd.ExecuteReader());
// fill report
rds = new ReportDataSource("InvoicesDataSet_InvoiceTable",dt);
reportViewerLocal.LocalReport.DataSources.Clear();
reportViewerLocal.LocalReport.DataSources.Add(rds);
At one point I noticed that the report was incomplete and it was missing one record. I've changed a few conditions so that the query would return exactly two rows and... surprise: The report shows only one row instead of two. I've tried to debug it to find where the problem is and I got stuck at
dt.Load(cmd.ExecuteReader());
When I've noticed that the DataReader contains two records but the DataTable contains only one. By accident, I've added an ORDER BY clause to the query and noticed that this time the report showed correctly.
Apparently, the DataReader contains two rows but the DataTable only reads both of them if the SQL query string contains an ORDER BY (otherwise it only reads the last one). Can anyone explain why this is happening and how it can be fixed?
Edit:
When I first posted the question, I said it was skipping the first row; later I realized that it actually only read the last row and I've edited the text accordingly (at that time all the records were grouped in two rows and it appeared to skip the first when it actually only showed the last). This may be caused by the fact that it didn't have a unique identifier by which to distinguish between the rows returned by MySQL so adding the ORDER BY statement caused it to create a unique identifier for each row.
This is just a theory and I have nothing to support it, but all my tests seem to lead to the same result.
After fiddling around quite a bit I found that the DataTable.Load method expects a primary key column in the underlying data. If you read the documentation carefully, this becomes obvious, although it is not stated very explicitly.
If you have a column named "id" it seems to use that (which fixed it for me). Otherwise, it just seems to use the first column, whether it is unique or not, and overwrites rows with the same value in that column as they are being read. If you don't have a column named "id" and your first column isn't unique, I'd suggest trying to explicitly set the primary key column(s) of the datatable before loading the datareader.
Just in case anyone is having a similar problem as canceriens, I was using If DataReader.Read ... instead of If DataReader.HasRows to check existence before calling dt.load(DataReader) Doh!
I had same issue. I took hint from your blog and put up the ORDER BY clause in the query so that they could form together the unique key for all the records returned by query. It solved the problem. Kinda weird.
Don't use
dr.Read()
Because It moves the pointer to the next row.
Remove this line hope it will work.
Had the same issue. It is because the primary key on all the rows is the same. It's probably what's being used to key the results, and therefore it's just overwriting the same row over and over again.
Datatables.Load points to the fill method to understand how it works. This page states that it is primary key aware. Since primary keys can only occur once and are used as the keys for the row ...
"The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist. When creating DataTable objects, the Fill operation normally creates only column name metadata. However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created." (http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx)
Came across this problem today.
Nothing in this thread fixed it unfortunately, but then I wrapped my SQL query in another SELECT statement and it work!
Eg:
SELECT * FROM (
SELECT ..... < YOUR NORMAL SQL STATEMENT HERE />
) allrecords
Strange....
Can you grab the actual query that is running from SQL profiler and try running it? It may not be what you expected.
Do you get the same result when using a SqlDataAdapter.Fill(dataTable)?
Have you tried different command behaviors on the reader? MSDN Docs
I know this is an old question, but for me the think that worked whilst querying an access database and noticing it was missing 1 row from query, was to change the following:-
if(dataset.read()) - Misses a row.
if(dataset.hasrows) - Missing row appears.
For anyone else that comes across this thread as I have, the answer regarding the DataTable being populated by a unique ID from MySql is correct.
However, if a table contains multiple unique IDs but only a single ID is returned from a MySql command (instead of receiving all Columns by using '*') then that DataTable will only organize by the single ID that was given and act as if a 'GROUP BY' was used in your query.
So in short, the DataReader will pull all records while the DataTable.Load() will only see the unique ID retrieved and use that to populate the DataTable thus skipping rows of information
Not sure why you're missing the row in the datatable, is it possible you need to close the reader? In any case, here is how I normally load reports and it works every time...
Dim deals As New DealsProvider()
Dim adapter As New ReportingDataTableAdapters.ReportDealsAdapter
Dim report As ReportingData.ReportDealsDataTable = deals.GetActiveDealsReport()
rptReports.LocalReport.DataSources.Add(New ReportDataSource("ActiveDeals_Data", report))
Curious to see if it still happens.
In my case neither ORDER BY, nor dt.AcceptChanges() is working. I dont know why is that problem for. I am having 50 records in database but it only shows 49 in the datatable. skipping first row, and if there is only one record in datareader it shows nothing at all.
what a bizzareeee.....
Have you tried calling dt.AcceptChanges() after the dt.Load(cmd.ExecuteReader()) call to see if that helps?
I know this is an old question, but I was experiencing the same problem and none of the workarounds mentioned here did help.
In my case, using an alias on the colum that is used as the PrimaryKey solved the issue.
So, instead of
SELECT a
, b
FROM table
I used
SELECT a as gurgleurp
, b
FROM table
and it worked.
I had the same problem.. do not used dataReader.Read() at all.. it will takes the pointer to the next row. Instead use directly datatable.load(dataReader).
Encountered the same problem, I have also tried selecting unique first column but the datatable still missing a row.
But selecting the first column(which is also unique) in group by solved the problem.
i.e
select uniqueData,.....
from mytable
group by uniqueData;
This solves the problem.

Crystal Reports setdatasource

Im setting the report datasource in my C# code like this:
report.SetDataSource(dataSource);
And it all works but after that the crystal report still tries to connect to the database.
I though that if i set the datasource from the code the report wont want to get more data from database then.
Is that really like so that even after setting the datasource from code report goes and asks more data from database?
EDIT: The real problem im facing is that i have 2 reports, one goes and fetches data from database after setting datasource from code and the other just doesn't. I have been pulling my hear all day to figure this out and no luck yet. The two reports are allmost identical, both use a database View to get the fields from database and both use the same code to set the datasource from code.
The logic is that i load the report in my code, get the required database fields from it and create DataTable with those fields. Then i fill it with data and set it as datasource with this: report.SetDataSource(dataSource);. For both reports the code and logic is identical! Just the fields are different..
In case if you have subreports.
loop through subreports
and call SetDataSource(dataSource) on each subreport object.
Report.RicevutaCup r = new Report.RicevutaCup();
System.Data.DataSet ds = CupDatasetFactory.CreaRicevutaCupDataSet(df);
r.OpenSubreport("RicevutaCup_Prestazioni").SetDataSource(ds);
r.SetDataSource(ds);
Solved my issue (after restarting IIS).

Categories

Resources