1

我最近刚刚安装了 Visual Studio 2012,我的 Crystal 报表在 2010 中一切正常,但是当我将代码迁移到 2012 时,我无法显示任何内容。您能否检查一下这段代码是否缺少我的东西:

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cnn;
            string connectionString = null;
            string sql = null;
            connectionString = "data source=server; initial catalog=DBO;user id=sa; password= passw0rd";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = "select BadgeNo as DataColumn1,Name as DataColumn2, Section as DataColumn3 from Safety where ID = '24'";
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            cnn.Close();

            DataSet1 ds = new DataSet1();
            dscmd.Fill(ds, "DataTable1");

            ReportobjRpt = new Report1 ();
            objRpt.SetDataSource(ds.Tables[0]);
            CrystalReportViewer1.ReportSource = objRpt;
            CrystalReportViewer1.RefreshReport();
        }

我正在使用数据集将数据从 SQL 服务器传输到水晶报表。请帮忙。谢谢你。

4

3 回答 3

1

试试这个方法:

using CrystalDecisions.CrystalReports.Engine;

public partial class Default2 : System.Web.UI.Page
{

  SqlConnection con = new SqlConnection(“Connection String “);
  protected void Page_Load(object sender, EventArgs e)
  {
    con.Open();
    ReportDocument rpt = new ReportDocument();
    rpt.Load(Server.MapPath("~/CrystalReport.rpt"));
    SqlCommand cmd = new SqlCommand("Select * from Raj_Table[tbl_name]", con);
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.SelectCommand = cmd;
    da.Fill(ds, "Raj_Table[TableName]");
    rpt.SetDataSource(ds);
    con.Close();
    CrystalReportViewer1.ReportSource = rpt;
    CrystalReportViewer1.DataBind();
  }
}

它会帮助你。

于 2014-05-14T05:59:05.360 回答
0

您正在引用对象objRpt,而不是ReportobjRpt已经创建的对象。

试试这个:

protected void Page_Load(object sender, EventArgs e)  
{
    SqlConnection cnn;
    string connectionString = null;
    string sql = null;
    connectionString = "data source=server; initial catalog=DBO;user id=sa; password= passw0rd";
    cnn = new SqlConnection(connectionString);
    cnn.Open();
    sql = "select BadgeNo as DataColumn1,Name as DataColumn2, Section as DataColumn3 from Safety where ID = '24'";
    SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
    cnn.Close();

    DataSet1 ds = new DataSet1();
    dscmd.Fill(ds, "DataTable1");

    ReportobjRpt = new Report1 ();
    ReportobjRpt.SetDataSource(ds.Tables[0]);
    CrystalReportViewer1.ReportSource = ReportobjRpt;
    CrystalReportViewer1.RefreshReport();
}
于 2015-02-16T08:24:12.267 回答
0

此代码将帮助您:

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cnn;
            string connectionString = null;
            string sql = null;
            connectionString = "data source=server; initial catalog=DBO;user id=sa; password= passw0rd";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            SqlCommand com = new SqlCommand("select BadgeNo as DataColumn1,Name as DataColumn2, Section as DataColumn3 from Safety where ID = '24'", conn);
            adap.SelectCommand = com;
            adap.Fill(tables);
            ReportDocument doc;                
            myreport.SetDataSource(tables);
            doc = new ReportDocument();
            doc.Load(Server.MapPath("RptName.rpt"));
            myreport.ReportSource = doc;
            myreport.ReportSource = myreport;
        }
于 2013-07-10T13:11:04.070 回答