如何使用visual studio 2012在ASP.net C#中生成水晶报表?

时间:2022-09-01 23:42:52

I just recently installed Visual Studio 2012, everything's working good with 2010 with my Crystal report but when I migrated the code to 2012 I can't display anything. Can you pls check if there is something I'm missing with this code:

我刚刚安装了Visual Studio 2012,一切都与2010年的Crystal报告一致,但是当我将代码迁移到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();
        }

I am using a dataset to transfer data from the SQL server to crystal report. Please help. Thank you.

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

3 个解决方案

#1


1  

Try this way:

试试这种方式:

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

It will help you.

它会帮助你。

#2


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;
        }

This code Will be help you

这段代码会对你有所帮助

#3


0  

You are referencing the object objRpt instead of ReportobjRpt which is already created.

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

Try this one:

试试这个:

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

#1


1  

Try this way:

试试这种方式:

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

It will help you.

它会帮助你。

#2


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;
        }

This code Will be help you

这段代码会对你有所帮助

#3


0  

You are referencing the object objRpt instead of ReportobjRpt which is already created.

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

Try this one:

试试这个:

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