Add data at design time using the Report Explorer.
To add Calculated Fields
Note: Do not use an equals sign (=) at the beginning of the formula as you would in the DataField property of a textbox. |
To add Parameters
Bind a report to a data source using the data source icon in the detail section band which opens the Report Data Source window. There are four tabs on the window for the four most commonly used data sources:
To use the OLE DB data source
To use the SQL data source
To use the XML data source
To use an Unbound data source
To create a data source in Visual Basic.NET
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste JUST ABOVE the ReportStart event. |
Copy Code |
---|---|
Dim conn As System.Data.OleDb.OleDbConnection Dim reader As System.Data.OleDb.OleDbDataReader |
Visual Basic.NET code. Paste INSIDE the ReportStart event. |
Copy Code |
---|---|
Dim dbPath As String = getDatabasePath() Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + "\\NWIND.mdb" conn = New System.Data.OleDb.OleDbConnection(connString) Dim cmdText As String = "SELECT Categories.*, Products.* FROM Products INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID WHERE Products.UnitPrice = 18" |
To create a data source in C#
The following example shows what the code for the method looks like.
C# code. Paste JUST ABOVE the ReportStart event. |
Copy Code |
---|---|
private static System.Data.OleDb.OleDbConnection conn; private static System.Data.OleDb.OleDbDataReader reader; |
C# code. Paste INSIDE the ReportStart event. |
Copy Code |
---|---|
string dbPath = getDatabasePath(); string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + "\\NWIND.mdb"; conn = new System.Data.OleDb.OleDbConnection(connString); string cmdText = "SELECT Categories.*, Products.* FROM Products INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID WHERE Products.UnitPrice = 18"; System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(cmdText, conn); conn.Open(); reader = cmd.ExecuteReader(); |
To close the data connection in Visual Basic
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste INSIDE the ReportEnd event. |
Copy Code |
---|---|
reader.Close() conn.Close() |
To close the data connection in C#
The following example shows what the code for the method looks like.
C# code. Paste INSIDE the ReportEnd event. |
Copy Code |
---|---|
reader.Close(); conn.Close(); |
Warning: Do not access the Fields collection outside the DataInitialize and FetchData events. Accessing the Fields collection outside of these events is not supported, and has unpredictable results. |
To add fields in Visual Basic
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste INSIDE the DataInitialize event. |
Copy Code |
---|---|
Fields.Add("CategoryID") Fields.Add("CategoryName") Fields.Add("ProductName") Fields.Add("UnitsInStock") Fields.Add("Description") Fields.Add("TotalLabel") |
To add fields in C#
The following example shows what the code for the method looks like.
C# code. Paste INSIDE the DataInitialize event. |
Copy Code |
---|---|
Fields.Add("CategoryID"); Fields.Add("CategoryName"); Fields.Add("ProductName"); Fields.Add("UnitsInStock"); Fields.Add("Description"); Fields.Add("TotalLabel"); |
To populate fields in Visual Basic
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste INSIDE the FetchData event. |
Copy Code |
---|---|
Try reader.Read() Me.Fields("CategoryID").Value = reader("categories.CategoryID") Me.Fields("CategoryName").Value = reader("CategoryName") Me.Fields("ProductName").Value = reader("ProductName") Me.Fields("UnitsInStock").Value = reader("UnitsInStock") Me.Fields("Description").Value = reader("Description") Me.Fields("TotalLabel").Value = "Total Number of " + reader("CategoryName") + ":" eArgs.EOF = False Catch eArgs.EOF = True End Try |
To populate fields in C#
The following example shows what the code for the method looks like.
C# code. Paste INSIDE the FetchData event. |
Copy Code |
---|---|
try { reader.Read(); Fields["CategoryID"].Value = reader["categories.CategoryID"].ToString(); Fields["CategoryName"].Value = reader["CategoryName"].ToString(); Fields["ProductName"].Value = reader["ProductName"].ToString(); Fields["UnitsInStock"].Value = reader["UnitsInStock"].ToString(); Fields["Description"].Value = reader["Description"].ToString(); Fields["TotalLabel"].Value = "Total Number of " + reader["CategoryName"].ToString() + ":"; eArgs.EOF = false; } catch { eArgs.EOF = true; } |
To use the IEnumerable data source
To create a data source in Visual Basic
Paste INSIDE the class declaration of the report |
Copy Code |
---|---|
Private datasource1 As IEnumerator(Of String) = Nothing Dim list As List(Of String) = Nothing |
Paste INSIDE the class declaration of the report |
Copy Code |
---|---|
Private Function GetIEnumerableData() As IEnumerable(Of String) For i As Integer = 1 To 10 list.Add(String.Format("TestData_{0}", i.ToString())) Next Return list End Function |
To create a data source in C#
Paste JUST ABOVE the InitializeComponent method |
Copy Code |
---|---|
private IEnumerator<string> datasource = null; |
Paste BELOW the InitializeComponent method |
Copy Code |
---|---|
private IEnumerable<string> GetIEnumerableData() { for (int i = 1; i <= 10; i++) { yield return string.Format("TestData_{0}", i.ToString()); } } |
To add fields in Visual Basic
Paste INSIDE the DataInitialize event |
Copy Code |
---|---|
Me.Fields.Add("TestField") Me.list = New List(Of String) datasource1 = GetIEnumerableData().GetEnumerator() |
To add fields in C#
Paste INSIDE the DataInitialize event |
Copy Code |
---|---|
this.Fields.Add("TestField"); datasource = GetIEnumerableData().GetEnumerator(); |
To populate fields in Visual Basic
Paste INSIDE the FetchData event |
Copy Code |
---|---|
If datasource1.MoveNext() Then Me.Fields("TestField").Value = datasource1.Current eArgs.EOF = False Else eArgs.EOF = True End If |
To populate fields in C#
Paste INSIDE the FetchData event |
Copy Code |
---|---|
if (datasource.MoveNext()) { this.Fields["TestField"].Value = datasource.Current; eArgs.EOF = false; } else eArgs.EOF = true; |