There are different ways to generate a report in C# using Visual Studio. In this
tutorial, I am going to show you how to achieve this goal using Report Wizard
available in Visual Studo 2010.
Now you have a new Windows Forms Application in Visual Studio 2010. Then you add a report to the project by right-clicking the name of the project under Solution Explorer. Select Add->New Item…Under Installed Template, select Reporting->Report Wizard. In the DataSet Properties dialog, you create a new data source by clicking the New… button .
In the next dialog, you choose Database. Then click the Next button.
In the Choose a Database Model dialog, you select Dataset. By clicking the Next button, you will see another dialog that allows you create a connection to your SQL Server database. Click New Connection… button to create a new connection to the database.
After you select a table of the database to be used on your report, you will be backed to the DataSet Properties dialog. Click Next. In the Arrange Fields dialog, you drag and drop fields of the table into the Values box.
Then click Next. Then click Finish to finish the report setup task. Now you have a report. To display the report on your form, you have to add a ReportViewer from the Toolbox to the form. Then on Load method of the form, you write code as shown below to bind data to the report dynamically. You will change database name, table name, server name, user id, and password to yours.
SqlConnection con = null;
SqlDataAdapter adapter = null;
DataSet ds = new DataSet();
string DbName = "your_database_name";
string TblName = "table_name";
string conStr = "Server=MyServer; Database=" + DbName + ";User=your_username;Password=your_password;";
con = new SqlConnection();
con.ConnectionString = conStr;
adapter = new SqlDataAdapter("Select * From " + TblName, con);
DataTable dt = new DataTable();
this.reportViewer1.Visible = true;
ReportDataSource rpds = new ReportDataSource();
rpds.Name = "DataSet1";
rpds.Value = dt;
reportViewer1.LocalReport.ReportPath = "Report.rdlc";
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: