通过PrintToPrinter打印我的水晶报告后丢失数据

时间:2012-05-07 09:47:45

标签: c# .net crystal-reports

使用PrintToPrinter打印文档后,所有数据都丢失了,不知道为什么????

这段代码还有一个问题,我已经通过添加:

纠正了这个问题
rpt.SetDatabaseLogon("u","p");

现在我打印了空文件。

这是我的代码:

protected void btnPrintToPrinter_Click(object sender, EventArgs e)
{
    int empno = Convert.ToInt32(Session["AnyVal"]);

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);



    string strQuery = "SELECT [View_EmplDetail].[Uni-Code], [View_EmplDetail].[FacultyCode], [View_EmplDetail].[EmpIDstr], [View_EmplDetail].[EngGivenName], [View_EmplDetail].[position_eng], [View_EmplDetail].[Emp_no], [View_EmplDetail].[GivenName], [View_EmplDetail].[position_name], [View_EmplDetail].[DariName], [Tbl_Dept].[EName], [View_EmplDetail].[photo] FROM   [MoHEDatabase].[dbo].[View_EmplDetail] [View_EmplDetail] INNER JOIN [MoHEDatabase].[dbo].[Tbl_Dept] [Tbl_Dept] ON [View_EmplDetail].[DepCode]=[Tbl_Dept].[DepCode] WHERE  [Emp_no] = @empno";
    SqlCommand command = new SqlCommand(strQuery, connection);
    command.CommandType = System.Data.CommandType.Text;

    command.Parameters.AddWithValue("@empno", empno);
    command.Connection = connection;
    command.Connection.Open();

    SqlDataAdapter da = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    da.Fill(dt);

    ReportDocument rpt = new ReportDocument();
    string _reportPath = Server.MapPath("..\\Student\\cardFinal.rpt");
    //rpt.Load(AppDomain.CurrentDomain.BaseDirectory + "\\" + @"\\Student\\CardFinal.rpt");
    rpt.Load(_reportPath);

    rpt.SetDataSource(dt);
    emp_card_report_viewer.ReportSource = rpt;

    string sq = "";

    //{View_OrgStr1.Uni-Code}=0 and {View_OrgStr1.FacultyCode}=119 
    //sq = "{View_StudentAddNew.Student_ID}=" + Session["AnyVal"];
    if (Session["AnyVal"].ToString() != "")
    {
        sq = "{View_EmplDetail.Emp_no}=" + int.Parse(Session["AnyVal"].ToString());
    }

    //emp_card_report.Report.FileName = "../Student/CardFinal.rpt";
    emp_card_report_viewer.SelectionFormula = sq;
    //ConnectionInfo connInfo = new ConnectionInfo();
    //connInfo.ServerName = "172.16.0.15";
    //connInfo.DatabaseName = "MoHEMISDatabase";
    //connInfo.UserID = "hemis_admin";
    //connInfo.Password = "hemis@sabir";
    //TableLogOnInfos crtt = new TableLogOnInfos();
    //TableLogOnInfo crtto = new TableLogOnInfo();
    //Tables crtables;
    //crtables = rpt.Database.Tables;
    //foreach (CrystalDecisions.CrystalReports.Engine.Table crtable in crtables)
    //{
    //    crtto = crtable.LogOnInfo;
    //    crtto.ConnectionInfo = connInfo;
    //    //crtable.ApplyLogInInfo(crtto);
    //}
    ConnectionInfo connInfo1 = new ConnectionInfo();
   // connInfo1.ServerName = "server";

    setDBLOGONforReport(connInfo1);



    //emp_card_report_viewer.RefreshReport();


    //ConnectionInfo connInfo = new ConnectionInfo();
    //connInfo.ServerName = "server";
    //connInfo.DatabaseName = "MoHEDatabase";
    //connInfo.UserID = "hemis_admin";
    //connInfo.Password = "hemis@sabir";



    //setDBLOGONforReport(connInfo);


    CrystalDecisions.Shared.PageMargins pageMargins = new
    CrystalDecisions.Shared.PageMargins(0, 0, 0, 0);
    rpt.PrintOptions.ApplyPageMargins(pageMargins);

    rpt.PrintOptions.PrinterName = printerList.SelectedItem.Value;
    emp_card_report_viewer.RefreshReport();

    rpt.PrintToPrinter(1, false, 1, 1);


    rpt.Close();
    rpt = null;
}

1 个答案:

答案 0 :(得分:1)

回答:

我在水晶报告中使用了“选择专家记录”,我删除了“选择专家”中的所有公式并且它有效....:)