Hi Bhavesh23,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table>
<tr>
<td>Id</td>
<td><asp:TextBox ID="txtId" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Last Name</td>
<td><asp:TextBox ID="txtLN" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>First Name</td>
<td><asp:TextBox ID="txtFN" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>DOB</td>
<td><asp:TextBox ID="txtDOB" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnPrint" runat="server" OnClick="OnPrint" />
</td>
</tr>
</table>
<CR:CrystalReportViewer ID="crPersonInformation" runat="server" AutoDataBind="true"
EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
Bindreport();
}
protected void Bindreport()
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"));
Employees dsPersonInformations = GetEmployees();
crystalReport.SetDataSource(dsPersonInformations);
crPersonInformation.ReportSource = crystalReport;
}
private Employees PrintEmployee()
{
Employees dsEmployee = new Employees();
DataTable dt = new DataTable("PrintEmployee");
dt.Columns.Add("EmployeeID");
dt.Columns.Add("LastName");
dt.Columns.Add("FirstName");
dt.Columns.Add("BirthDate");
dt.Rows.Add(txtId.Text, txtLN.Text, txtFN.Text, txtDOB.Text);
dsEmployee.Tables.Add(dt);
return dsEmployee;
}
protected void OnPrint(object sender, EventArgs e)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"));
Employees dsPrint = PrintEmployee();
crystalReport.SetDataSource(dsPrint.Tables["PrintEmployee"]);
crPersonInformation.ReportSource = crystalReport;
crystalReport.PrintToPrinter(1, false, 0, 0);
Bindreport();
}
private Employees GetEmployees()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand("SELECT EmployeeID,LastName,FirstName,BirthDate FROM Employees");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (Employees dsEmployees = new Employees())
{
sda.Fill(dsEmployees, "Employee");
return dsEmployees;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Bindreport()
End Sub
Protected Sub Bindreport()
Dim crystalReport As ReportDocument = New ReportDocument()
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"))
Dim dsPersonInformations As Employees = GetEmployees()
crystalReport.SetDataSource(dsPersonInformations)
crPersonInformation.ReportSource = crystalReport
End Sub
Private Function PrintEmployee() As Employees
Dim dsEmployee As Employees = New Employees()
Dim dt As DataTable = New DataTable("PrintEmployee")
dt.Columns.Add("EmployeeID")
dt.Columns.Add("LastName")
dt.Columns.Add("FirstName")
dt.Columns.Add("BirthDate")
dt.Rows.Add(txtId.Text, txtLN.Text, txtFN.Text, txtDOB.Text)
dsEmployee.Tables.Add(dt)
Return dsEmployee
End Function
Protected Sub OnPrint(ByVal sender As Object, ByVal e As EventArgs)
Dim crystalReport As ReportDocument = New ReportDocument()
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"))
Dim dsPrint As Employees = PrintEmployee()
crystalReport.SetDataSource(dsPrint.Tables("PrintEmployee"))
crPersonInformation.ReportSource = crystalReport
crystalReport.PrintToPrinter(1, False, 0, 0)
Bindreport()
End Sub
Private Function GetEmployees() As Employees
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,LastName,FirstName,BirthDate FROM Employees")
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dsEmployees As Employees = New Employees()
sda.Fill(dsEmployees, "Employee")
Return dsEmployees
End Using
End Using
End Using
End Function