Click here to Skip to main content
15,848,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have to declare a function for Crystal Report Viewer in C#, but it shows me some error like; (Object Does not contain a definition for Database and no extension method 'Database' excepting a first argument of type 'object' could be find (are you missing a using directive or assembly reference?))
my code is given below
C#
public static object ConnectionForReports(object objReport )
{
    SqlConnection cn = null;
    cn = new SqlConnection(Properties.Settings.Default.CMScon);
    CrystalDecisions.Shared.TableLogOnInfo logOnInfo = null;
    logOnInfo = objReport.Database.Tables[0].LogOnInfo; // <-- here
    logOnInfo.ConnectionInfo.ServerName = cn.DataSource;
    logOnInfo.ConnectionInfo.DatabaseName = cn.Database;
    logOnInfo.ConnectionInfo.UserID = "sa";
    logOnInfo.ConnectionInfo.Password = "SQLadmin";
    objReport.Database.Tables[0].ApplyLogOnInfo(logOnInfo); // <-- and here
    return objReport;
}

The Error shows in Highlighted line.
Posted
Updated 23-Dec-14 22:05pm
v2
Comments
Agent__007 24-Dec-14 1:39am    
How are you calling you ConnectionForReports() method? Cast objReport object to the type you are passing in and you should be good.

Look at your definition for objReport:
C#
public static object ConnectionForReports(object objReport )

The object class does not have a Database property, so the compiler complains.
You need to cast the parameter to the appropriate datatype which does have the property, is all.
 
Share this answer
 
Comments
MaroofQaiser 24-Dec-14 3:13am    
which type of parameter to the apprppriate datatype? i don't understand your logic
OriginalGriff 24-Dec-14 3:54am    
You method has a parameter called "objReport" yes?
You have said that it is of datatype "object" yes?

The object class has very few properties, and none of them are called "Database".

You need to cast it to the type of class that does have a Database property.
What class is that? I don't know - I can't see your screen. So you need to look at what you are passing to the method when you call it, and that might give you a clue. I can't do that for you!
MaroofQaiser 24-Dec-14 5:22am    
i have used a datatype name ReportDocument, it has a property of database,
public static object ConnectionForReports(ReportDocument objReport)
{
logOnInfo = objReport.Database.Tables[0].LogOnInfo;
}
it works perfectly, but when i use this method on a button event,
protected void CmdPreview_Click(object sender, ImageClickEventArgs e)
{
frmInquiryReports objReport = new frmInquiryReports();
objReport = MethodClass.ConnectionForReports(objReport);
}
it shows me error like: "The best overload method matche for ConnectionforReports(CrystalDecisions.CrystalReports.Engine.ReportDocument) has some invalid Arguements"
OriginalGriff 24-Dec-14 5:36am    
Try using the full name in the method declaration:

public static object ConnectionForReports(CrystalDecisions.CrystalReports.Engine.ReportDocument objReport)

It may be that you have another class somewhere called ReportDocument and the system is getting confused.
I declare a method in my class
public static object ConnForReport(ReportDocument objReport)
{
    SqlConnection cn = null;
    cn = new SqlConnection(Properties.Settings.Default.CMScon);
    CrystalDecisions.Shared.TableLogOnInfo logOnInfo = null;
    logOnInfo = objReport.Database.Tables[0].LogOnInfo;

    logOnInfo.ConnectionInfo.ServerName = cn.DataSource;
    logOnInfo.ConnectionInfo.DatabaseName = cn.Database;
    logOnInfo.ConnectionInfo.UserID = "sa";
    logOnInfo.ConnectionInfo.Password = "SQLadmin";
    objReport.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
    return objReport;
}


then i use this method in my button click event like this:
C#
protected void CmdPreview_Click(object sender, ImageClickEventArgs e)
       {
           try
           {
           InquiryReports objReport = new InquiryReports();
            string vSelectionFormula = null;
ReportDocument objReports = (ReportDocument)MethodClass.ConnForReport(objReport);
vSelectionFormula = "{CompalintView.ComplaintStatus}='" + CboCompStatus.Text + "'";
            InquiryRptViewer.SelectionFormula = vSelectionFormula;
            InquiryRptViewer.ReportSource = objReports;
            Session.Add("CR_Session",objReports);
           }
           catch (Exception ex)
           {

           }

       }


Do Not forget to write these code on your page_load event:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session.Remove("CR_Session");
                CboComplaintStatusFill();
            }
            else
            {
                InquiryRptViewer.ReportSource = Session["CR_Session"];
            }
        }


If any Query so comment me... i have try these code in my project it works perfectly. Thanx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900