报告没有显示在报表查看器了 [英] Report not showing up on report viewer
问题描述
我开发一个 C#
应用程序,我需要生成一个报告。我是一个使用填充有从传来的数据的数据集存储过程
这需要一个参数从C#code。我创建的 report1.rdlc
参数,并从一个文本框中输入数据填充它。当我运行该应用程序,我不能看到报表查看器什么。
公共无效GenerateBranchwiseReport()
{
conn.Open();
的SqlCommand BranchReportcmd =新的SqlCommand(从masterlookup选择[AM / BSI名],其中[ASC类型] ='分公司'GROUP BY [AM / BSI名称],conn);在
SqlDataReader的BranchReportread = BranchReportcmd.ExecuteReader();
而(BranchReportread.Read())
{
BranchManagerName.Add(BranchReportread.GetValue(0)的ToString());
}
conn.Close();
的foreach(在BranchManagerName串managername)
{
conn.Open();
的SqlCommand GetReportDatacmd =新的SqlCommand();
GetReportDatacmd.CommandText =USP_BranchwiseReport;
GetReportDatacmd.CommandType = CommandType.StoredProcedure;
GetReportDatacmd.Parameters.Add(新的SqlParameter(@ BranchManagerName,managername));
GetReportDatacmd.Connection =康恩;
GetReportDatacmd.ExecuteNonQuery();
SqlDataAdapter的大=新SqlDataAdapter的(GetReportDatacmd);
DataSet的DS =新的DataSet();
da.Fill(DS);
conn.Close();
reportViewer1.Reset();
this.reportViewer1.Visible = TRUE;
字符串REPORTNAME = @D:\\用户\\管理员\\文档\\ Visual Studio 2010的\\项目\\ ReportwithParameter \\ ReportwithParameter \\ Report1.rdlc
this.reportViewer1.LocalReport.ReportPath = @D:\\用户\\管理员\\文档\\ Visual Studio 2010的\\项目\\ ReportwithParameter \\ ReportwithParameter \\ Report1.rdlc
ReportParameter [] =参数新ReportParameter [1];
参数[0] =新ReportParameter(ManagerName,managername);
this.reportViewer1.LocalReport.SetParameters(参数);
ReportDataSource ReportBranch =新ReportDataSource(DatasetWithParameter.USP_BranchwiseReport,ds.Tables [0]);
this.reportViewer1.LocalReport.ReportEmbeddedResource = REPORTNAME;
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(新Microsoft.Reporting.WinForms.ReportDataSource(DatasetWithParameter.USP_BranchwiseReport,ds.Tables [0]));
//this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
this.reportViewer1.LocalReport.Refresh();
//发电子邮件();
}
}
要在你的C#的WinForm项目中使用报表查看以下code添加到一个按钮或在矿井的Form_Load:
字符串strConnectionString =数据源=(本地);初始目录= Projects_DB;集成安全性=真;
DataSet的DS =新的DataSet();
SqlDataAdapter的大=新的SqlDataAdapter();
CMD的SqlCommand =新的SqlCommand(sp_GetProject+0660CAD6-6F1A-4D19-A1FD-17BF3655AC98');
cmd.CommandType = CommandType.Text;
cmd.Connection =新的SqlConnection(strConnectionString);
da.SelectCommand = CMD; da.Fill(DS,数据集1); reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(新ReportDataSource(数据集1,ds.Tables [0]));
reportViewer1.LocalReport.Refresh();
reportViewer1.RefreshReport();
假设你有一个接受@ProjectID参数的存储过程。
你必须设置 cmd.CommandType = CommandType.Text
如果你使用SQL命令一起传递这些参数。但是,如果你不想传递参数只是改变命令类型为 cmd.CommandType = CommandType.StoredProcedure
。
以下是在本实施例中使用的存储过程:
CREATE PROC [DBO]。[sp_GetProject]
@ProjectID为nvarchar(50)= NULL如
开始 选择专案编号,项目名FROM项目
哪里
(@ProjectID IS NULL)OR(专案编号= @ProjectID)结束
I am developing a c#
application where I need to generate a report. I am a using a dataset which is filled with the data coming from a stored procedure
which takes one parameter from the C# code. I am creating a parameter in report1.rdlc
and populating it with the data from a text box. When I run the application I can’t see anything on report viewer.
public void GenerateBranchwiseReport()
{
conn.Open();
SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
while (BranchReportread.Read())
{
BranchManagerName.Add(BranchReportread.GetValue(0).ToString());
}
conn.Close();
foreach (string managername in BranchManagerName)
{
conn.Open();
SqlCommand GetReportDatacmd = new SqlCommand();
GetReportDatacmd.CommandText = "USP_BranchwiseReport";
GetReportDatacmd.CommandType = CommandType.StoredProcedure;
GetReportDatacmd.Parameters.Add(new SqlParameter("@BranchManagerName", managername));
GetReportDatacmd.Connection = conn;
GetReportDatacmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
reportViewer1.Reset();
this.reportViewer1.Visible = true;
string reportname = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
this.reportViewer1.LocalReport.ReportPath = @"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
ReportParameter[] param = new ReportParameter[1];
param[0] = new ReportParameter("ManagerName", managername);
this.reportViewer1.LocalReport.SetParameters(param);
ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
//this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
this.reportViewer1.LocalReport.Refresh();
//SendEmail();
}
}
To use the report viewer in your C# WinForm project add the following code to a button or inthe form_load:
string strConnectionString = "Data Source=(local);Initial Catalog=Projects_DB;Integrated Security=True";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("sp_GetProject " + "'0660CAD6-6F1A-4D19-A1FD-17BF3655AC98'");
cmd.CommandType = CommandType.Text;
cmd.Connection = new SqlConnection (strConnectionString);
da.SelectCommand = cmd;
da.Fill(ds,"DataSet1");
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));
reportViewer1.LocalReport.Refresh();
reportViewer1.RefreshReport();
Assuming that you have a stored procedure that accepts @ProjectID parameter.
You have to set the cmd.CommandType = CommandType.Text
if you pass these parameters along with the sql command. However, if you don't want to pass parameters just change the commandType to cmd.CommandType = CommandType.StoredProcedure
.
Below is the stored procedure used in this example:
CREATE PROC [dbo].[sp_GetProject]
@ProjectID nvarchar(50)=NULL
AS
BEGIN
SELECT ProjectID, ProjectName FROM Projects
WHERE
(@ProjectID IS NULL) OR (ProjectID = @ProjectID)
END
这篇关于报告没有显示在报表查看器了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!