我最近刚刚安装了 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();
}
我正在使用数据集将数据从 SQL 服务器传输到水晶报表。请帮忙。谢谢你。
试试这个方法:
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();
}
}
它会对你有帮助。
您正在引用对象
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();
}
此代码将为您提供帮助:
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;
}