Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with cr and DataSets

Status
Not open for further replies.

lutzs

Programmer
Oct 8, 2002
75
LU
Hi!

For many days I'm concerning with the following problem:
I have a .net application with a crystal report.
There is a dataSet with 3 tables. Two tables are from my database. For the third table I've created a DataTable with a column.
My problem is representing contents of this third table.
I must create a DataView to show the value with a dataGrid.
Without a DataView the column is empty.
Why is the column empty?
How can I represent the DataView on my CrystalReport?
In CR I can add the database fields on my table. But there are just DataTables no DataViews.

It would be great, if someone can help me.
Thanks, Stephanie
 
Can you post the code that creates the third DataTable? I just solved a similar problem myself.

Ben
 
Hi,

here's the code. Can you help me?

//*** new table ***
DataTable ergebnis = new DataTable();

DataColumn myDataColumn = new DataColumn();

myDataColumn.ColumnName = "kunde";
myDataColumn.DataType = typeof(System.String);
myDataColumn.DefaultValue = "test";

// add column to datatable
ergebnis.Columns.Add(myDataColumn);

// add datatable to dataset
dataSet.Tables.Add(ergebnis);

// add dataSet to crystalreport
oRpt.SetDataSource(dataSet);
crystalReportViewer1.ReportSource = oRpt;
 
When you're dealing with DataColumns, you'll usually only have access to things that describe the column itself, not its data. If you want to add data into it, connect it to a table and traverse it through the rows property.

I think the reason you're getting no data is because you're associating it with that table. Try adding your string to each field with something like this:

Code:
// add column to datatable
ergebnis.Columns.Add(myDataColumn);

for (int r = 0;r<ergebnis.Rows.Count - 1;r++)
{
     ergebnis.Rows(r).Item(&quot;kunde&quot;) = &quot;test&quot;
}

I'm not 100% sure of my syntax; I've never actually written this in any language but VB... hopefully it will give you the right idea though.

The exception to this is when you want to give the column an expression... for instance, if you had a table with 3 columns of integers and you wanted the fourth column to add up the total. Then you can use the datacolumn's expression property to set it up. If you're just trying to add literal text, use the method above.

Hope this helps... if not, let me know and I'll continue to work with you on it!

Ben
 
Hi Ben,

When I use your code I get an error message:
&quot;System.Data.DataTable.Rows' denotes a 'property' where a 'method' was expected&quot;

It was noticeable to me that I can't view the values of &quot;new&quot; DataTables.
If I choose a DataTable with existing Data from my Database, it isn't a problem for the CrystalReportViewer.
The problem is: if I create a new DataTable with a DataColumn and a DefaultValue.

If your code is correct or you can tell me what's wrong, perhaps it's the answer.

Thanks so much for your help.

Stephanie
 
Ok, I think I may have figured it out...

Microsoft in its infinite wisdom has decided that you have to manually add rows to a &quot;new&quot; table. If you don't, then you have a table with 1 column and 0 rows, which displays in my datagrid as &quot;null&quot;. Adding a column just tells the computer that there is the ability to have a certain type of field at this point, it doesn't actually create one... so if you need 40 rows, you'll have to run some kind of loop to create a row 40 times.

I don't know how to add rows in C++/C#. My projects have been strictly Visual Basic. I don't know if it will help, but here is the code that worked in VB:
Code:
        Dim t As New Data.DataTable()
        t.Columns.Add(&quot;Thing1&quot;, GetType(Integer))
        Dim y As Data.DataRow = t.NewRow
        r.Item(&quot;Thing1&quot;) = 17
        t.Rows.Add(r)
        DataSet.Tables.Add(t)
Like I said, I don't know how to &quot;translate&quot; that for you... but your code should look fairly similar to this. The above example only adds a single row to the table with the value 17 in it.
 
Hi Ben,

I've &quot;translated&quot; the code in C#:

// create DataTable and DataColumn
DataTable t = new DataTable();
DataColumn myDataColumn = new DataColumn();
myDataColumn.ColumnName = &quot;name&quot;;
myDataColumn.DataType = typeof(System.String);

// add DataColumn to DataTable
t.Columns.Add(myDataColumn);

// create DataRow
DataRow myDataRow = t.NewRow();
// the value of the DataRow
myDataRow[&quot;name&quot;] = &quot;testblabla&quot;;

// add DataRow to DataTable and DataTable to DataSet
t.Rows.Add(myDataRow);
dataSet.Tables.Add(t);

Is it correct?

Output:
In my DataGrid are two rows:
Row1 = &quot;testblabla&quot;
Row2 = null

When I add the databasefield &quot;ergebnis&quot; with the Database Expert on my report, I can't see something.
Is the value null? Why?

I´ve still noticed the following:
when I open the report with Crystal Report, not in VS .net, and I click the preview, there is a window:
&quot;ADO.net(XML): Please enter connection information:&quot;
There are two possibilities:
1) browse to xml file path: I can choose the dataset.xsd file. The report is empty.
2) Visual Studio data Class name: nameofapplication.DataSet1
When I click finish I get the message: &quot;Logon failed. Details: object reference not set to an instance of an object&quot;.
Is it normal?

I hope that you don't lose the overview and you can get me
tips.

Thanks, Stephanie


 
Hello again,

I think I understand your question; if not, this would probably be useless information.

You've got the data showing up in the table, but you can't get it to appear on the report? If that's the case, it's very easy to solve:

(More code to &quot;translate&quot;)
Code:
      Dim rpt As New ReportName()
      rpt.SetDataSource(DatasetName)
      CrystalReportViewer.ReportSource = rpt

Change ReportName to the name of your Crystal Report file and DatasetName to the name of your Dataset. This idea works with the option of using the XML file that you mentioned. The XML file only records the structure of the data, not any actual records. So you were on the right track when you did that... but your code has to provide the data to the report. See if that works for you, and if not, we'll keep trying!

Ben
 
Hi!

Yes, I can show the data in the table (dataGrid) but not on the report.

That's my code:
...
// add the table dtt to the dataSet
dataSet.Tables.Add(dtt);

// Output DataGrid
dataGrid1.DataSource = dtt;

// Output CrystalReports
CrystalReport1 oRpt = new CrystalReport1();
oRpt.SetDataSource(dataSet.dtt);
crystalReportViewer1.ReportSource = oRpt;

That's like your example in VB. But the report is empty. I can just see the report header and the details section in cr is empty.
Perhaps, is there another instruction missing?

Thanks, Stephanie

 
This is going to sound strange, but we're dealing with Microsoft here... <grin>

Try rebuildiing your report now... remove all data connections and redo them. I ran into this several times... I don't know how complicated it will be to do it, but something about the data connections into CR doesn't allow it to update itself, so if you change the structure of the data at some point, the report will have to be manually reset to handle it, even though you haven't changed the file name. It's stupid, I know, but that's all I can figure out... so try re-creating the report (from scratch if you have the time) and see if it displays properly.

I hope this works for you!

Ben
 
Hi Ben!

It doesn't works!
I've deleted the report and removed all connections.

The strange is that contents of the tables I_KUNDE and I_BESTAND (from the oracle database) are indicated with CrystalReport. And i can show the values of DTT with a dataGrid.

Here's my code. Perhaps, you can find the error:

Dataset1 dataSet = new Dataset1();

OleDbConnection oleConn = new OleDbConnection (&quot;Provider=MSDAORA.1;Password=pwd;User ID=user;Data Source=db&quot;);
OleDbDataAdapter daI_KUNDE= new OleDbDataAdapter(&quot;SELECT ...&quot;, oleConn);
OleDbDataAdapter daI_BESTAND = new OleDbDataAdapter(&quot;SELECT ... &quot;, oleConn);

daI_KUNDE.Fill (dataSet, &quot;i_KUNDE&quot;);
daI_BESTAND.Fill (dataSet, &quot;i_BESTAND&quot;);

//*** new table DTT ***
DataTable dtt = new DataTable();
dtt.Columns.Add(&quot;kunde&quot;);
dtt.Columns.Add(&quot;bestand&quot;);

foreach(DataRow dr_kunde in dataSet.I_KUNDE.Rows)
{
foreach(DataRow dr_bestand in dataSet.I_BESTAND.Rows)
{
string KundeKundenID = Convert.ToString(dr_kunde[&quot;kunde_n_int&quot;]);
string BestandKundenID = Convert.ToString(dr_bestand[&quot;kunde_n_int&quot;]);

if (KundeKundenID == BestandKundenID)
{
DataRow dtr = dtt.NewRow();
dtr[&quot;kunde&quot;] = dr_kunde[&quot;kunde_n_int&quot;];
dtr[&quot;bestand&quot;] = dr_bestand[&quot;bestand_n_int&quot;];
dtt.Rows.Add(dtr);
}
}
}

// add table to dataSet
dataSet.Tables.Add(dtt);

// * * * OUTPUT * * *
dataGrid1.DataSource = dtt;

// CR
CrystalReport1 oRpt = new CrystalReport1();
oRpt.SetDataSource(dataSet);
crystalReportViewer1.ReportSource = oRpt;


Thanks, Stephanie
 
I'm sorry, Stephanie...

I can't see anything wrong with it. The only possibility I can suggest is this:

Is &quot;dtt&quot; the only table that should display on the report? If so, try just sending the one table as the datasource... like so:
Code:
CrystalReport1 oRpt = new CrystalReport1(); 
oRpt.SetDataSource(dtt); 
crystalReportViewer1.ReportSource = oRpt;
If that doesn't work, re-post your problem and see if someone else can help you... I'm not sure what to try next.

Sorry I couldn't be more helpful!

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top