Creating Dataset for RDLC Reports

Intermediate 20 min read Lesson 2 of 5

What is a Dataset?

A dataset is like a container that holds your data for the report. It defines the structure of your data - what columns you have and what type of data they hold.

Simple Explanation

Think of a dataset as a blueprint for your data. It tells the report: "Here are the columns you can use, like Name, Price, and Category."

Creating a Typed Dataset

Step 1 — Add New DataSet

  1. Right-click your project in Solution Explorer
  2. Select Add → New Item
  3. Choose DataSet from the Data folder
  4. Name it: ProductDataSet.xsd

Step 2 — Define DataTable

// In the DataSet designer:
1. Right-click on the design surface
2. Choose Add → DataTable
3. Name it: Products
4. Add these columns:
   - ProductId (System.Int32)
   - Name (System.String)
   - Price (System.Decimal)
   - Description (System.String)
   - Category (System.String)

Step 3 — Create DataTable in Code

You can also create a DataTable directly in your C# code:

// Create DataTable programmatically
public DataTable GetProductDataTable()
{
    // Create a new DataTable called "Products"
    DataTable table = new DataTable("Products");
    
    // Define the columns
    table.Columns.Add("ProductId", typeof(int));
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Price", typeof(decimal));
    table.Columns.Add("Description", typeof(string));
    table.Columns.Add("Category", typeof(string));
    
    // Add some sample data
    table.Rows.Add(1, "Laptop", 999.99, "High-performance laptop", "Electronics");
    table.Rows.Add(2, "Mouse", 29.99, "Wireless mouse", "Electronics");
    
    return table;
}

Step 4 — Fill Dataset from SQL Server

public DataSet GetProductsFromDatabase()
{
    // Create a new DataSet
    DataSet dataSet = new DataSet();
    
    // Connect to your database
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        // Write your SQL query
        string query = "SELECT ProductId, Name, Price, Description, Category FROM Products";
        
        // Create a DataAdapter to fill the DataSet
        using (SqlDataAdapter adapter = new SqlDataAdapter(query, conn))
        {
            // Fill the DataSet with data from the database
            adapter.Fill(dataSet, "Products");
        }
    }
    
    return dataSet;
}
Key Takeaway

Datasets define the structure of your report data. You can create them manually or fill them from a database.

Test Your Knowledge - Take Quiz