Link to home
Start Free TrialLog in
Avatar of RakeshBhandari
RakeshBhandariFlag for India

asked on

providing sql username & password to crystal report in asp.net c#

i have a crystal report in asp.net & c#

the rpt file has the sql query with groupings as i require

what i want to do is to provide ONLY the sql username & password using C# code

please note i want to supply just the username & password... NOT wanting to fill the report with a dataset or whatever
CrystalReportViewer1.ReportSource = "BrokerageConsolidatedMonthHO.rpt";
CrystalReportViewer1.SelectionFormula = "Month ({table1.cmonth})=5" + ;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dhanasekaran Sengodan
Dhanasekaran Sengodan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please refer to the enclosed method, it can change the datasource, userid, password and databasename of a ReportDocument (support sub-report)

and you can get there information from web.config
            System.Data.SqlClient.SqlConnectionStringBuilder SConn = new System.Data.SqlClient.SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
           
            SetCRLogOnInfo(rptDoc, SConn.DataSource, SConn.UserID, SConn.Password, SConn.InitialCatalog);
        public void SetCRLogOnInfo(ReportDocument mainInRD, string dataSource, string userId, string pwd, string DatabaseName)
        {
            //do the main reports database
            TableLogOnInfo logonInfo = null;

            foreach (CrystalDecisions.CrystalReports.Engine.Table table in mainInRD.Database.Tables)
            {
                logonInfo = table.LogOnInfo;
                logonInfo.ConnectionInfo.ServerName = dataSource;
                logonInfo.ConnectionInfo.DatabaseName = DatabaseName;
                logonInfo.ConnectionInfo.UserID = userId;
                logonInfo.ConnectionInfo.Password = pwd;
                table.ApplyLogOnInfo(logonInfo);
            }
            try
            {
                //now update logon info for all sub-reports
                if (!mainInRD.IsSubreport && mainInRD.Subreports != null && mainInRD.Subreports.Count > 0)
                {
                    foreach (ReportDocument rd in mainInRD.Subreports)
                    {
                        SetCRLogOnInfo(rd, dataSource, userId, pwd, DatabaseName);
                    }
                }
            }
            catch
            {
            }
        }

Open in new window

Avatar of RakeshBhandari

ASKER

thank you!