Hi akhter,
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
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="True"
OnSelectedIndexChanged="gvEmployees_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="EmployeeID" ReadOnly="True" HeaderText="Pack_No" />
<asp:TemplateField HeaderText="Packno">
<ItemTemplate>
<a href="BalePack.aspx?PID=<%#Eval("EmployeeID") %>">
<%# Eval("EmployeeID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="City" runat="server" Text='<%#Bind("City")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="Date" runat="server" Text='<%#Bind("BirthDate","{0:MM/dd/yyyy}")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<hr />
<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();
if (!IsPostBack)
{
BindGrid();
}
}
protected void gvEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
string PID = gvEmployees.SelectedRow.Cells[1].Text;
Response.Redirect("Packrpt.aspx?PID=" + PID);
}
private void BindGrid()
{
gvEmployees.DataSource = GetData("SELECT EmployeeID,FirstName + ' ' + LastName As Name,BirthDate,City FROM Employees");
gvEmployees.DataBind();
}
protected void Bindreport()
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"));
Employees dsPersonInformations = GetEmployees();
crystalReport.SetDataSource(dsPersonInformations);
crPersonInformation.ReportSource = crystalReport;
}
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;
}
}
}
}
private static DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet dsEmployees = new DataSet())
{
sda.Fill(dsEmployees, "Employee");
return dsEmployees;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Bindreport()
If Not IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub gvEmployees_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim PID As String = gvEmployees.SelectedRow.Cells(1).Text
Response.Redirect("Packrpt.aspx?PID=" & PID)
End Sub
Private Sub BindGrid()
gvEmployees.DataSource = GetData("SELECT EmployeeID,FirstName + ' ' + LastName As Name,BirthDate,City FROM Employees")
gvEmployees.DataBind()
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 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
Private Shared Function GetData(ByVal query As String) As DataSet
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dsEmployees As DataSet = New DataSet()
sda.Fill(dsEmployees, "Employee")
Return dsEmployees
End Using
End Using
End Using
End Function