Click here to Skip to main content
14,694,371 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Am using the code below to generate my report and it works alright. but the report keeps asking for username and password. how do i avoid this. please help me out thanks.
private void btnshow_Click(object sender, EventArgs e)
        { 
            
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings   
            ["ConnectionString"]);
            conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=MUCGPROJECT;User  
            ID=sa;Password=mike";
           
            
     SqlCommand cmd = new SqlCommand(string.Format("SELECT * FROM tblCollectorsRegistration     
            WHERE Collectorid='{0}'", this.txtCollectorid.Text), conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                
                if (dr.HasRows)
                {
                    conn.Close();
                    DataTable tbl = new DataTable();
                    conn.Open();
    SqlDataAdapter adp = new SqlDataAdapter("SELECT Collectorid, Title, Surname, Firstname, 
                    Middlename, Gender, Dateofbirth, Nationality, Religion, Maritalstatus,   
                    Spousename, Telephone, Postaladdress, Residentialaddress, Hometownaddress  
                    from tblCollectorsRegistration WHERE Collectorid = '" +     
                    this.txtCollectorid.Text + "'", conn);
                    
                    adp.Fill(tbl);
                    rptCollectorindividual objRpt = new rptCollectorindividual();
                    objRpt.Database.Tables[0].SetDataSource(tbl);
                    crystalReportViewer1.ReportSource = objRpt;
                    crystalReportViewer1.Refresh();
                }
                else
                {
                    // Id already present
                    MessageBox.Show("The Collector ID does not Exist");
                    return;
                   
                }
        }
Posted
Updated 12-Jun-13 4:47am
v4
Comments
Kschuler 12-Jun-13 14:10pm
   
Good answer, but instead of a comment you should make it a solution, as Sunasara Imdadhusen says.
Sunasara Imdadhusen 12-Jun-13 9:49am
   
do not post answer in comment box, if it is really answer then add into solution box

raeeschaudhary's answer in the comments is fine, but that might not be the problem. If you're a newbie to Crystal, have you verified that ALL your connections in both the report and subreport are pointing to the same DB with the same login? And if so, and if raeeschaudhary's answer doesn't help, how old are the reports? There are problems with dynamically updating connection data in very old reports, but there is a solution that you can read about here: http://scn.sap.com/community/crystal-reports-for-visual-studio/blog/2011/09/02/when-to-use-the-replace-connection-method-using-the-crystal-reports-or-inproc-ras-sdk-for-net
   
ReportDocument r=new ReportDocument
r.Load("path");
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.DatabaseName = "dbname";
connectionInfo.UserID = "userid";
connectionInfo.Password = "pwd";
SetDBLogonForReport(connectionInfo, r);
crystalReportViewer1.ReportSource = r;
crystalReportViewer1.RefreshReport();
   
Comments
mikeoabban 14-Jun-13 23:35pm
   
i added the code below and encountered two problems.
1, SetDBLogonForReport(crConnectionInfo, r); //error message does not exist in the current context
2, i already have his which loads the report using sql query
rptCollectorindividual objRpt = new rptCollectorindividual();
objRpt.Database.Tables[0].SetDataSource(tbl);

and i addeda new path which gives an error. that should i do
r.Load("D:\\my project\\Drms\\Ghpservice\\rptCollectorindividual.rpt");

\\ the codes
ReportDocument r = new ReportDocument();
r.Load("D:\\my project\\Drms\\Ghpservice\\rptCollectorindividual.rpt");

ConnectionInfo crConnectionInfo = new ConnectionInfo();
crConnectionInfo.DatabaseName = "MUCGPROJECT";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "mike";
SetDBLogonForReport(crConnectionInfo, r);//does not exist in the current context


crystalReportViewer1.ReportSource = r;
crystalReportViewer1.RefreshReport();

TableLogOnInfos crtablelognoinfos = new TableLogOnInfos();
TableLogOnInfo crtablelognoinfo = new TableLogOnInfo();

Tables CrTables;

CrTables = r.Database.Tables;


foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtablelognoinfo = CrTable.LogOnInfo;
crtablelognoinfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtablelognoinfo);
}
raeeschaudhary 18-Jun-13 7:02am
   
did u added using these three statement as headers
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;
did u added using these three statement as headers
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;
   

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