Skip to Content
0
Former Member
Aug 18, 2015 at 01:28 PM

How to Bind .NET Objects as DataSource to a Report? (DataSourceException)

1067 Views

Hello,

I'm already trying for hours and I still can't produce a Report from scratch with a .NET Object.

Everytime I call the SetDataSource-Method, I Get the DataSourceException:

I'm using Visual Studio 2013 Update 5 with CRforVS_13_0_13

Failed to load database information.

Error in File temp_54f4ace5-3d4b-49f4-a633-cb7098cbf8bd 10164_1776_{D1ADA617-8049-497D-BD87-2530EA8EEE81}.rpt:

Stack:

at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)

at CrystalDecisions.CrystalReports.Engine.ReportDocument.SetDataSourceInternal(Object val, Type type)

at CrystalDecisions.CrystalReports.Engine.ReportDocument.SetDataSource(DataSet dataSet)

at WindowsApplication12.Form1.Button1_Click(Object sender, EventArgs e) in c:\users\danielr\documents\visual studio 2013\Projects\WindowsApplication12\WindowsApplication12\Form1.vb:line 18

at System.Windows.Forms.Control.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

at System.Windows.Forms.Control.WndProc(Message& m)

at System.Windows.Forms.ButtonBase.WndProc(Message& m)

at System.Windows.Forms.Button.WndProc(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

What I'm doing:

I have a simple WinForms Application in which is just a Button. In the Click-Event I Initialize the Report, a List(Of TestClass) and a DataSet out of the List. Then I'm setting the DataSource and the Exceptionis thrown. For the Report I made a XSD File out of the List first, then generated the Report and set the XSD as DataSourceConnection for the Report.

Here is the SourceCode:

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

''Initialize

Dim testData = GenerateTestData()

Dim dataSet = ListToDataSet(testData)

Dim report = New CrystalReport1()

''Make a XSD File out of the DataSet for the Report (this step is just made initially)

'Dim writer = New IO.StreamWriter("Test.xsd")

'dataSet.WriteXmlSchema(writer)

'writer.Close()

''Set the DataSource for the Report

report.SetDataSource(dataSet)

''Export it to a PDF and then open it

report.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, "test.pdf")

Process.Start("test.pdf")

''Exit the Application

Application.Exit()

End Sub


''' <summary>

''' Generates some TestData

''' </summary>

''' <returns></returns>

''' <remarks></remarks>

Public Function GenerateTestData() As List(Of TestClass)

Dim r = New Random()

Dim list = New List(Of TestClass)

For i As Integer = 0 To 100

list.Add(New TestClass() With {.Betrag = r.Next(),

.Name = "TestName" & i})

Next

Return list

End Function


''' <summary>

''' Makes a DataSet

''' </summary>

''' <typeparam name="T"></typeparam>

''' <param name="dataList"></param>

''' <returns></returns>

''' <remarks></remarks>

Public Function ListToDataSet(Of T)(dataList As IEnumerable(Of T)) As DataSet

Dim dataTable = New DataTable(GetType(T).Name)

Dim propInfos() As PropertyInfo = Nothing

For Each rec As T In dataList

If propInfos Is Nothing Then

propInfos = rec.GetType().GetProperties()

For Each propInfo In propInfos

Dim columnType = propInfo.PropertyType

If columnType.IsGenericType AndAlso columnType.GetGenericTypeDefinition = GetType(Nullable(Of )) Then

columnType = columnType.GetGenericArguments(0)

End If

dataTable.Columns.Add(New DataColumn(propInfo.Name, columnType))

Next

End If

Dim dataRow As DataRow = dataTable.NewRow()

For Each propInfo In propInfos

dataRow(propInfo.Name) = If(propInfo.GetValue(rec, Nothing), DBNull.Value)

Next

dataTable.Rows.Add(dataRow)

Next

Dim dataSet = New DataSet()

dataSet.Tables.Add(dataTable)

Return dataSet

End Function

End Class


The Test Class is quite simple:

Public Class TestClass

Property Name As String

Property Betrag As Integer

End Class

The Report is just made out of the Standard Dialogue (New Item => Crystal Report => OK => Selected the TestClass DataSet => Next and so on) and looks like this

Could someone please help me?

Attachments

reportTemp.png (8.0 kB)