Creating Crystal report with C# and Sql server

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I'm using Crystal report to generate reports to my application, this is the code i used:

private void button5_Click(object sender, EventArgs e)     {         ReportDocument cryRpt = new ReportDocument();          TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();         TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();         ConnectionInfo crConnectionInfo = new ConnectionInfo();         Tables CrTables;          cryRpt.Load("C:\\Documents and Settings\\Administrateur\\Mes documents\\MyApplication\\MyApplication\\CrystalReport1.rpt");          crConnectionInfo.ServerName = ".\\SQLEXPRESS";         crConnectionInfo.DatabaseName = "database";         crConnectionInfo.UserID = "";         crConnectionInfo.Password = "";         crConnectionInfo.IntegratedSecurity = true;         CrTables = cryRpt.Database.Tables;          foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)         {             crtableLogoninfo = CrTable.LogOnInfo;             crtableLogoninfo.ConnectionInfo = crConnectionInfo;             CrTable.ApplyLogOnInfo(crtableLogoninfo);         }          cryRpt.SetDatabaseLogon("", "", ".\\SQLEXPRESS", "database");         crystalReportViewer1.ReportSource = cryRpt;         crystalReportViewer1.Refresh();     }

But when i run the application, a login screen appeared and demands the connection Id and password, i tried to enter null values but the connection is failed.

Where is the problem??

回答1:

You have to initialize an instance of the DataSet class, and fill it with information from

your DataSet because Crystal report Datasource is based on a dataset. This is a basic code of

using Crystal report with Dataset:

SqlConnection con = new SqlConnection();         con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=YOUR PATH\database.mdf;Integrated Security=True;User Instance=True";         con.Open();         string sql = "SELECT * FROM tablename";         SqlDataAdapter dscmd = new SqlDataAdapter(sql, con);         DataSet ds = new DataSet();         dscmd.Fill(ds, "tablename");         con.Close();          CrystalReport1 objRpt = new CrystalReport1();         objRpt.SetDataSource(ds.Tables["tablename"]);         crystalReportViewer1.ReportSource = objRpt;         crystalReportViewer1.Refresh();


回答2:

while we are creating the crystal report using windows explorer we have to provide the location of database ex: "C:\data" then once after that it works perfect but if we change the location of database file or the location of application it asks for login. The refresh of the datasource may also resolve your problem

consider this exapmle and try to resolve your problems here



回答3:

First goto database expert and check right side whether there is a single connection.if one or more connections there delete unwanted connection like access connections then copy and paste the below code it works perfectly

 ReportDocument cryRpt = new ReportDocument();              TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();             TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();             ConnectionInfo crConnectionInfo = new ConnectionInfo();             Tables CrTables;              string path = "" + Application.StartupPath + "\\Mgc\\Invoice.rpt";             cryRpt.Load(path);              cryRpt.SetParameterValue("billno", Program.billno);              crConnectionInfo.UserID = "userid";             crConnectionInfo.Password = "Password";             crConnectionInfo.ServerName = "servernme";              crConnectionInfo.DatabaseName = "database;               CrTables = cryRpt.Database.Tables;             foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)             {                 crtableLogoninfo = CrTable.LogOnInfo;                 crtableLogoninfo.ConnectionInfo = crConnectionInfo;                 CrTable.ApplyLogOnInfo(crtableLogoninfo);             }                   crystalReportViewer1.ReportSource = cryRpt;                 crystalReportViewer1.Refresh();


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!