Create Your First RDLC (Local SSRS) Report in ASP.NET using DataSet C#

Following steps demonstrate how to create your first RDLC Report. This blog is divided into 2 parts:-

  1. Prepare the report from a single table and to solve the Report Viewer Configuration Error, if encountered.
  2. Prepare a single report from two different tables (Not-Related).

The database used here is Microsoft’s Northwind database.

Prepare a Report from a Single Table

# Add DataSet.

# Add DataTable
# Add Columns
# Add RDLC Report
# Choose DataSet
# Choose FieldsĀ 
# Choose Layout
# Choose Style
# Finish the Wizard. The report will be then displayed
# Add ReportViewer to the ASP.NET page
# Following is the designer code snippet after adding the ReportViewer control
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head runat="server">
		<title></title>
	</head>
	<body>

<form id="form1" runat="server">
			<asp:ScriptManager ID="ScriptManager1" runat="server">
			</asp:ScriptManager>
			<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600">
			</rsweb:ReportViewer>
		</form>

	</body>
</html>
# Modifying the Code Behind section
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        Customers dsCustomers = GetData("select top 20 * from customers");
        ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}

private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"]
                                              .ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}

# Next Compile the code. There’s a chance that you might encounter the following error

and then when you click Continue, you’ll be finally redirected to your page which will be displaying the following error message.

The error is pretty straight-forward. Just as the message suggests add the following code to the Web.Config file

<system.webServer>
	<validation validateIntegratedModeConfiguration="false" />
	<handlers>
		<add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
	</handlers>
</system.webServer>

 

Plz make sure that your PublicKeyToken is same as that of the Microsoft.ReportViewer.WebForms in the assemblies section of system.web.

# Next again run the code. You’ll be able to see the following result

You can see that field, Country, has appeared twice. This is because of the fact that I had done a GroupBy this field.

That’s it from this blog. Next as a continuation of it, I’ll be demonstrating how to display multiple tables(2) in a single report. You can find the link of that blog very shortly.
Thanks…

Advertisements

Add a CheckBox to DataTable and DataGridView

To add a Column with CheckBox, in a DataGridView, do the following:
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "chkBxName";
checkColumn.HeaderText = "Check Box";
checkColumn.ReadOnly = false;

// if the datagridview is resized (on form resize) 
// the checkbox won't take up too much; value is 
// relative to the other columns' fill values
checkColumn.FillWeight = 10;

dataGridView1.Columns.Add(checkColumn);
It’s even simpler to add the CheckBox column to a DataTable and then, set it as a source to a DataGridView. Just set the type of a DataColumn to Boolean.
DataTable dt = new DataTable();

//this will show checkboxes
dt.Columns.Add(new DataColumn("Select", typeof(bool)));
dt.Columns.Add(new DataColumn("SomeText", typeof(string)));

dataGridView1.DataSource = dt;

Search a DataRow from DataTable1 into DataTable2

Tip

The situation is something like this. A GridView is getting populated at the PageLoad event. Now one of the Column of the Grid contains the CheckBox to represent the True/False state for that Row. There are 2 tables in the SQL Database. Now, if a DataRow is present in both the tables then, that Row’s checkbox should be marked in the GridView. The GridView Columns which contains the checkbox is being added at the runtime i.e, that column is not present in the SQL DataTable.
The question is how can I search a single DataRow of a DataTable against all the DataRows of another DataTable. Look at the code below


DataTable dt = null;
DataTable dtParent = null;

dt = GetList(); // Get the selected List
dtParent = GetNameList(); //Get the Entire List

// Mark a Column(s) as Primary, which will be searched for the given value.
dtParent.PrimaryKey = new DataColumn[] { dtParent.Columns["ID"] };
foreach (DataRow dr in dt.Rows)
{
	//If that Row is Present in dtParent then, mark the row's checkbox as checked.
	dtParent.Rows.Find(dr["ID"])["chkCheckBox"] = true; 
}
gvStudentDetails.DataSource = dtParent;
gvStudentDetails.DataBind();

Happy Coding …

DataTable’s ImportRow & Clone methods [C#]

Discussion

Here, I will demonstrate the functionality of DataTable’s ImportRow & Clone methods and the difference between the Rows.Add & ImportRow.

Clone method will copy the structure of the existing table, only Columns and Schema, not the data. So in the following

DataTable dt = GetSomeVal();
DataTable dt1 = dt.Clone();
dt1 will have exactly the same Column type as of dt. However, dt1.Rows.Count will return 0.As for the other issue look at the following code::


DataTable dt1 = new DataTable();
DataRow dr1 = dt1.NewRow();
DataTable dt2 = new DataTable();

// will throw an error as dr1 already belongs to another datatable
dt2.Rows.Add(dr1); 

// will execute adding a new row to dt2 Table
dt2.ImportRow(dr1);

dt1.Rows.Add(dr1);

The above code is pretty much self-explanatory. If you have to add a DataRow from a different DataTable use ImportRow method otherwise use Rows.Add.

Happy Coding …