Page 1 of 2

Relations between different datasets not working

Posted: Tue Oct 06, 2015 8:26 am
by peterson
Hello,
I am trying to connect 2 datasets which both have multiple tables and also have a relation. To give you an idea of the data involved here is the datascheme i'm using:

Dataset customers:
table customers
table orders

Dataset detail:
table orderdetails
The tables customers and orders have a one to many relation.
This is the code I use to attach the data source to stimulsoft

Code: Select all

StiReport report = new StiReport();
DataSet set = new DataSet();
set.ReadXmlSchema("customer.xsd");
 set.ReadXml("customer.xml");
            

DataSet set2 = new DataSet();
set2.ReadXmlSchema("detail.xsd");
set2.ReadXml("detail.xml");

report.RegData("dataset1", set);
report.RegData("dataset2", set2);
When I run this I can create a master-detail report for the customers and there orders tables which works great!
Then I tried to create a relation between the product table and the ordersdetails table and I get the following error:
Parent Data Source 'Orders' and Child Data Source 'detail' is not located in one DataSet and can't be used in relation 'Name'! You can use property CacheAllData of report to cache this Data Source to one DataSet.
After which I added the following line of code after instantiating the StiReports object:

Code: Select all

report.CacheAllData = true;
But if I then try to create the same master-details report and for the customers en details tables I get the following error when I try to preview it:
Cannot remove unique constraint 'Constraint1'. Remove foreign key constraint 'Customer_Orders' first.
Am I doing something not supported by Stimulsoft or is it a small bug?

Edit: oh yeah, heres the stack trace of the exception:

Code: Select all

   at System.Data.UniqueConstraint.CanBeRemovedFromCollection(ConstraintCollection constraints, Boolean fThrowException)
   at System.Data.ConstraintCollection.Remove(Constraint constraint)
   at System.Data.DataTable.set_PrimaryKey(DataColumn[] value)
   at System.Data.ConstraintCollection.Clear()
   at Stimulsoft.Report.Dictionary.StiDictionary.Disconnect()
   at Stimulsoft.Report.Engine.StiRenderProviderV2.Render(StiReport report, StiRenderState state)
   at Stimulsoft.Report.Engine.StiReportV2Builder.RenderSingleReport(StiReport masterReport, StiRenderState renderState)
   at Stimulsoft.Report.StiReport.RenderReport(StiRenderState renderState)
   at Stimulsoft.Report.StiReport.Render(StiRenderState renderState, StiGuiMode guiMode)
   at Stimulsoft.Report.StiReport.RenderWithWpf(Boolean showProgress)
   at Stimulsoft.Report.WpfDesign.StiWpfDesignerPreviewControl.buttonRefresh_Click(Object sender, RoutedEventArgs e)

Re: Relations between different datasets and cachealldata no

Posted: Tue Oct 06, 2015 8:42 am
by peterson
Hello, just found out that if I add the following code after loading the first dataset it works.

Code: Select all

set.Tables[1].Constraints.Clear();
set.Tables[0].Constraints.Clear();
So I get think that when you click the preview button stimulsoft tries to delete the constrains? But doesn't do it in the right order so it throws an exception.

Hope this might help you.

Re: Relations between different datasets not working

Posted: Tue Oct 06, 2015 2:34 pm
by HighAley
Hello.

Sorry, but we couldn't reproduce your issue.
If you still need our help, we need more detailed description of your issue.
Maybe you could send us a sample project that reproduces the issue.

Thank you.

Re: Relations between different datasets not working

Posted: Wed Oct 07, 2015 9:45 am
by peterson
Sure no problem, Do you mind if I PM it to you? Rather not post it publicly in case it still contains traces of company data.

Re: Relations between different datasets not working

Posted: Wed Oct 07, 2015 10:00 am
by Alex K.
Hello,

You can send it on support@stimulsoft.com

Thank you.

Re: Relations between different datasets not working

Posted: Wed Oct 07, 2015 10:14 am
by peterson
Aleksey wrote:Hello,

You can send it on support@stimulsoft.com

Thank you.
I've already sended it too you, my bad.
Should I also send it to support?

Re: Relations between different datasets not working

Posted: Wed Oct 07, 2015 12:06 pm
by HighAley
Hello.

We didn't get an exception before because we were testing your code on .Net Framework 4.
All versions above throws the exception. Even when preview an empty report. We can't say why this is happened.
Please, use your solution to avoid the exception.

Thank you.

Re: Relations between different datasets not working

Posted: Thu Oct 08, 2015 11:39 am
by peterson
Aleksey Andreyanov wrote:Hello.

We didn't get an exception before because we were testing your code on .Net Framework 4.
All versions above throws the exception. Even when preview an empty report. We can't say why this is happened.
Please, use your solution to avoid the exception.

Thank you.
Thanks, just tested it and this seems like a workaround.
Are you however planning to get it working with .net framework 4.5?

Re: Relations between different datasets not working

Posted: Thu Oct 08, 2015 1:48 pm
by HighAley
Hello.
peterson wrote:Are you however planning to get it working with .net framework 4.5?
We had got many troubles with different bugs of .Net Framework.
Some of them can be fixed from our side, some of them couldn't be fixed.
The fix of the issue is not on our side. We can't clear Constraints.
The the only way out in this case is to use additional code on your side.

Thank you.

Re: Relations between different datasets not working

Posted: Fri Oct 09, 2015 8:55 am
by peterson
Ah I see.

I've found a workaround for it, in case anybody else has the same problem

Code: Select all

        private void removeConstraintsAndPrimaryKeys(DataSet set)
        {
            removeContraints(set);
            removePrimaryKeys(set);
        }

        private void removeContraints(DataSet set)
        {
            set.EnforceConstraints = false;
            for (int i = 0; i < set.Tables.Count; i++)
            {
                
                for (int j = 0; j < set.Tables[i].Constraints.Count; j++)
                {
                    DataTable table = set.Tables[i];
                    
                    Constraint constraint = table.Constraints[j];
                    
                    if (table.Constraints.CanRemove(constraint))
                    {
                        table.Constraints.RemoveAt(j);
                        i = 0;
                        j = 0;
                    }
                    
                }
            }   
        }
        private void removePrimaryKeys(DataSet set)
        {
            for(int i =0; i < set.Tables.Count; i++)
            {
                set.Tables[i].PrimaryKey = null;
            }
        }
Example usage:

Code: Select all

Dataset set = new Dataset();
set.readXML("text.xml");
removeConstraintsAndPrimaryKeys(set);