Reputation: 27
I am new to C# but i am trying to create class to load all reports and pass the report name for it
public void ReportQuery(string DTTable,string query, ReportClass RPTT )
{
using (IDbConnection db = new SqlConnection(constring))
{
db.Open();
da = new SqlDataAdapter(query, con);
da.Fill(ds, DTTable);
RPTT rpt= new RPTT();
rpt.Load("~/RPTSales.rpt");
rpt.SetDataSource(ds.Tables[DTTable]);
this.crystalReportViewer1.ReportSource = rpt;
}
}
I get error on RPTT rpt= new RPTT()
;
'RPTT' is a variable but is used like a type
How can I pass the report name as parameter ?
Edit : this is the original code which i am trying to create class for it since i am loading lots of reports using it:
using (IDbConnection db = new SqlConnection(constring))
{
if (ds.Tables["RPTSales"] != null)
{ds.Tables["RPTSales"].Clear();}
db.Open();
da = new SqlDataAdapter("select * from sales", con);
da.Fill(ds, "RPTSales");
Reports.RPTSales rpt = new Reports.RPTSales();
rpt.Load("~/RPTSales.rpt");
rpt.SetDataSource(ds.Tables["RPTSales"]);
SetDBLogonForReport(cn, rpt);
this.crystalReportViewer1.ReportSource = rpt;
}
after edit the class code and once i try to call it :
ReportQuery("RPTSales", "select * from sales",Reports.RPTSales );
i get error about Reports.RPTSales
Error 'RPTSales' is a type, which is not valid in the given context
Upvotes: 0
Views: 278
Reputation: 1403
Your original post had little context around its usage, and you said RRPT is report "name", but i assume you mean it is the subclass?
In that case, use templating: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-methods
Something like this (untested):
public void ReportQuery<T>(string DTTable,string query) where T : ReportClass
{
using (IDbConnection db = new SqlConnection(constring))
{
db.Open();
da = new SqlDataAdapter(query, con);
da.Fill(ds, DTTable);
T rpt= new T(); // Or just `T rpt = T()` if error here
rpt.Load("~/RPTSales.rpt");
rpt.SetDataSource(ds.Tables[DTTable]);
this.crystalReportViewer1.ReportSource = rpt;
}
Very little context in orginal post, but does that seem to answer your actual question?
Upvotes: 1
Reputation: 46249
On original code Reports.RPTSales
is a Type,So you might create a instance be a parameter to method.
ReportQuery("RPTSales", "select * from sales",new Reports.RPTSales());
In ReportQuery
method you can use rpt
object to do data databinding.
public void ReportQuery(string DTTable,string query, Reports.RPTSales rpt)
{
using (IDbConnection db = new SqlConnection(constring))
{
db.Open();
da = new SqlDataAdapter(query, con);
da.Fill(ds, DTTable);
rpt.Load("~/RPTSales.rpt");
rpt.SetDataSource(ds.Tables[DTTable]);
this.crystalReportViewer1.ReportSource = rpt;
}
}
Upvotes: 1
Reputation: 3424
Your RPTT
is already an instance of the class. You're trying to use an instance of the class as if it were a class itself. Instead of trying to do
RPTT rpt= new RPTT();
Just use the RPTT parameter.
public void ReportQuery(string DTTable,string query, ReportClass RPTT )
{
using (IDbConnection db = new SqlConnection(constring))
{
db.Open();
da = new SqlDataAdapter(query, con);
da.Fill(ds, DTTable);
RPTT.Load("~/RPTSales.rpt");
RPTT.SetDataSource(ds.Tables[DTTable]);
this.crystalReportViewer1.ReportSource = RPTT;
}
Upvotes: 2