Programatically Rendering a Report
I am working on an application that renders a report (.rdlc) to a user based on criteria entered. Upon clicking a button I build a paramaterized call to a SQL Stored procedure. The data is returned to a dataset and I use that dataset to propulate a report. The problem is I can see data in the dataset/datatable, and the report looks like it is being initialized/filled with the retreved data but it does not render the report in teh reportviewer. Here is the .XSD:
(I had to shortcut the url in the schema due to forum issues. It sthe standard namespace stuff.)
<?xml version="1.0"?>
<xs:schema id="ReportData" xmlns="" xmlns:xs="XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="ReportData" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ReportRecords">
<xs:complexType>
<xs:sequence>
<xs:element name="UserID" msdata:ReadOnly="true" type="xs:int" minOccurs="0" />
<xs:element name="EmployeeName" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
<xs:element name="WorkTeam" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
<xs:element name="ManagerName" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
<xs:element name="ProjectID" msdata:ReadOnly="true" type="xs:int" minOccurs="0" />
<xs:element name="ProjectName" msdata:ReadOnly="true" type="xs:string" minOccurs="0" />
<xs:element name="Hours" type="xs:decimal" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
And the report was generated using a wizard from this xsd.
Here is the code called after the button is clicked:
Sub GetData2()
'step 1. get the data using a stored procedure and store to a dataset
Dim connectionString As String = System.Configuration.ConfigurationManager.Connecti onStrings("appConnectionString").ConnectionString
Dim myConnection As SqlConnection = New SqlConnection(connectionString)
Dim myCommand As SqlCommand = New SqlCommand("dbo.usp_GetMangersReportData", myConnection)
Dim myDataSet As DataSet = New DataSet()
Dim myStartDate As DateTime = txtStartDate.Text
Dim myEndDate As DateTime = txtEndDate.Text
Dim mySelectedValue As String = DropDownListWorkTeam.SelectedItem.Text
If mySelectedValue = "" Then
mySelectedValue = " "
End If
myCommand.Parameters.Clear()
myCommand.CommandType = Data.CommandType.StoredProcedure
'supply the parameters
With myCommand
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@StartDate", SqlDbType.VarChar, 12)
.Parameters("@StartDate").Value = myStartDate.ToString("yyyy-MM-dd")
.Parameters.Add("@EndDate", SqlDbType.VarChar, 12)
.Parameters("@EndDate").Value = myEndDate.ToString("yyyy-MM-dd")
.Parameters.Add("@WorkTeam", SqlDbType.VarChar, 100)
.Parameters("@WorkTeam").Value = mySelectedValue
.Parameters.Add("@ManagerId", SqlDbType.Int)
.Parameters("@ManagerId").Value = DropDownListManager.SelectedValue
.Parameters.Add("@EmployeeId", SqlDbType.Int)
.Parameters("@EmployeeId").Value = DropDownListEmployee.SelectedValue
End With
Dim myadapter As New SqlDataAdapter(myCommand)
Dim myDataTable As DataTable = New DataTable("ReportRecords")
'define the table columns
Dim column As DataColumn
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
With column
.DataType = System.Type.GetType("System.Int32")
.ColumnName = "UserID"
.ReadOnly = True
.Unique = False
End With
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(column)
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
With column
.DataType = System.Type.GetType("System.String")
.ColumnName = "EmployeeName"
.ReadOnly = True
.Unique = False
End With
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(column)
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
With column
.DataType = System.Type.GetType("System.String")
.ColumnName = "WorkTeam"
.ReadOnly = True
.Unique = False
End With
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(column)
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
With column
.DataType = System.Type.GetType("System.String")
.ColumnName = "ManagerName"
.ReadOnly = True
.Unique = False
End With
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(column)
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
With column
.DataType = System.Type.GetType("System.Int32")
.ColumnName = "ProjectID"
.ReadOnly = True
.Unique = False
End With
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(column)
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
With column
.DataType = System.Type.GetType("System.String")
.ColumnName = "ProjectName"
.ReadOnly = True
.Unique = False
End With
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(column)
' Create new DataColumn, set DataType, ColumnName
' and add to DataTable.
column = New DataColumn()
With column
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "Hours"
.ReadOnly = True
.Unique = False
End With
myConnection.Open()
myadapter.Fill(myDataTable)
'add our table to the dataset
myDataSet.Tables.Add(myDataTable)
'name the dataset for later
myDataSet.DataSetName = "ReportData"
'step 2. move data from dataset to the report xsd file
'The path to the RDLC (report layout and binding description). This is
'set to be copied locally with the app (ClickOnce required), so we can use
'the app run location as the path.
With ReportViewer1
.ProcessingMode = ProcessingMode.Local
'the datasource names match the .xsd file node names
.LocalReport.DataSources.Add(New ReportDataSource("ReportData", myDataSet.Tables("ReportRecords")))
.LocalReport.ReportPath = System.AppDomain.CurrentDomain.BaseDirectory & "rptManagersReport.rdlc"
.Visible = True
.LocalReport.ExecuteReportInCurrentAppDomain(Syste m.Reflection.Assembly.GetExecutingAssembly().Evide nce)
.LocalReport.Refresh()
End With
myConnection.Close()
End Sub
Any ideas? I am at a loss.
|