Hi adnanali,
Here in your code if you are assigning ReportViewer ProcessingMode also the LocalReport ReportPath but you have not used any Datasource value to assign report viewer. Because of that error is raising. If you will place an debugger in your IsPostBack code you will find the error after ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report2.rdlc") line.
If you don’t want to bind ReportViwer on page load the simply add those two line before BindReport Method call in TextChanged event.
Refer the below code for your reference. also implement it as per your code.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
private void BindReport(DataSet dsCustomers)
{
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
protected void TextChanged(object sender, EventArgs e)
{
string country = txtCountry.Text.Trim();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
BindReport(GetFilteredDataSet("Customers_GetCustomersCountryWise", country));
}
private DataSet GetFilteredDataSet(string procedure, string country)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
if (!string.IsNullOrEmpty(country))
{
cmd.Parameters.Add(new SqlParameter("@CountryName", txtCountry.Text.Trim()));
}
cmd.Connection = con;
cmd.CommandText = procedure;
cmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
}
return ds;
}
}
private Customers 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 (Customers dsCustomers = new Customers())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
End If
End Sub
Private Sub BindReport(dsCustomers As DataSet)
Dim datasource As New ReportDataSource("Customers", dsCustomers.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End Sub
Protected Sub TextChanged(sender As Object, e As EventArgs)
Dim country As String = txtCountry.Text.Trim()
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
BindReport(GetFilteredDataSet("Customers_GetCustomersCountryWise", country))
End Sub
Private Function GetFilteredDataSet(procedure As String, country As String) As DataSet
Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim cmd As New SqlCommand()
If Not String.IsNullOrEmpty(country) Then
cmd.Parameters.Add(New SqlParameter("@CountryName", txtCountry.Text.Trim()))
End If
cmd.Connection = con
cmd.CommandText = procedure
cmd.CommandType = CommandType.StoredProcedure
Dim ds As New DataSet()
Using da As New SqlDataAdapter(cmd)
da.Fill(ds)
End Using
Return ds
End Using
End Function
Private Function GetData(query As String) As Customers
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dsCustomers As New Customers()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Function