Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
crystal report keeps asking for user name and password. Am using SQL query to generate the report, how do i provide this user name and password in the codes. i tried this but not working.please help me out

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();

ReportDocument cryRpt = new ReportDocument();
            TableLogOnInfos crtablelognoinfos = new TableLogOnInfos();
            TableLogOnInfo crtablelognoinfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;
            cryRpt.Load("D:\\my project\\Drms\\Ghpservice\\rptCollectorindividual.rpt");
            crConnectionInfo.ServerName = "USER-PC";
            crConnectionInfo.DatabaseName = "MUCGPROJECT";
            crConnectionInfo.UserID = "sa";
            crConnectionInfo.Password = "mike";
            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtablelognoinfo = CrTable.LogOnInfo;
                crtablelognoinfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtablelognoinfo);
            }
                }
                else
                {
                    // Id already present
                    MessageBox.Show("The Collector ID does not Exist");
                    return;
                   
                }
Posted
Updated 10-Jun-13 20:33pm
v2

1 solution

Hello,
You need to pass server credentials through code like this :
C#
rptDoc.SetDatabaseLogon(yourDatabaseServerUsername, yourDatabaseServerPassword);

Example :
C#
rptDoc.SetDatabaseLogon("sa", "123");
 
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