导航下一页时在Crystal Report中获取“数据库登录错误”

时间:2015-10-23 11:23:20

标签: c# asp.net crystal-reports dataset

我在ASP.NET和C#(Dataset)中为Crystal Report开发了以下代码, 我在报告中有多个页面我正在获得第一页正确但在导航下一页时我收到登录错误。

public partial class WSDayBookReport : System.Web.UI.Page

{
    SQLClass sqlfunc = new SQLClass();
    ReportDocument rdoc = new ReportDocument();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (Session["CRpt"] != null)
            {
                rdoc =(ReportDocument) Session["CRpt"];
            }
            CrystalReportViewer1.ReportSource = rdoc;
            CrystalReportViewer1.RefreshReport();
        }
        else
        {
            // 'NOT POSTBACK
        }
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        if (Session["CRpt"] != null)
        {
            Session.Remove("CRpt");
        }
        Session["CRpt"]=rdoc;
    }

protected void btnSubmit_Click(object sender, EventArgs e)
{
    DayBookDataSet dset = new DayBookDataSet();
    //OpeningStock();
    string qry = "select  i.ItemGroupID, ig.ItemGroupCode As ItemGroupCode, Sum(iss.RegularStock) As OpeningStock from ItemStock iss, Item i, ItemGroup ig where ig.ItemGroupID = i.ItemGroupID AND iss.ItemID = i.ItemId group by i.ItemGroupID,ig.ItemGroupCode";
    sqlfunc.GetDataFromDT(dset.ItemGroupStock, qry, "sqlcon");
    var q1 = from igs in dset.ItemGroupStock.AsEnumerable()
             select igs;
    foreach (var res in q1)
    {
        res.ConsumedStock = 0;
        res.ClosingStock = 0;
    }
    // loadUser();
    qry = "select pp.PersonnelParticularID As UserID, pp.PersonnelName As UserName from WineOrder wo, WineOrderDetail wod, PersonnelParticular pp, ItemGroup ig, Item i where pp.PersonnelParticularID = wo.IssuedToPersonnelID AND ig.ItemGroupID = i.ItemGroupID AND i.ItemID = wod.ItemID AND wod.IsOrderCancelled = 0 AND wod.WineOrderId = wo.WineOrderId AND wo.WineOrderDate='" + Convert.ToDateTime(txtDate.Text).ToString("MM/dd/yyyy") + "' group by pp.PersonnelParticularID,pp.PersonnelName";
    sqlfunc.GetDataFromDT(dset.UserMaster, qry, "sqlcon");
    if (dset.UserMaster.Rows.Count == 0)
    {
        lblStatus.Text = "No Consumption on selected date.";
        CrystalReportViewer1.ReportSource = null;
        if (Session["ssnRdoc"] != null)
        {
            Session.Remove("ssnRdoc");
        }

        return;
    }
    else
    {
        lblStatus.Text = "";
    }

    //user consumption
    qry = "select  pp.PersonnelParticularID As UserID,ig.ItemGroupID, sum(wod.OrderQty) As ConsumptionQty from WineOrder wo, WineOrderDetail wod, ItemGroup ig, Item i, PersonnelParticular pp where pp.PersonnelParticularID = wo.IssuedToPersonnelID AND ig.ItemGroupID = i.ItemGroupID AND i.ItemID = wod.ItemID AND wod.IsOrderCancelled = 0 AND wod.WineOrderId = wo.WineOrderId AND wo.WineOrderDate='" + Convert.ToDateTime(txtDate.Text).ToString("MM/dd/yyyy") + "' group by ig.ItemGroupID, pp.PersonnelParticularID";
    sqlfunc.GetDataFromDT(dset.UserConsume, qry, "sqlcon");
    dset.AcceptChanges();
    ViewState.Add("vsDset", dset);
    //Deducting new RU Entry after the entered date...
    DataTable dtTemp = new DataTable();
    string qry2 = "select Sum(rs.Quantity) As Qty,i.ItemGroupID from RegularStock rs, Item i where i.ItemID = rs.ItemID AND rs.RSDate>='" + Convert.ToDateTime(txtDate.Text).ToString("MM/dd/yyyy") + "' Group by i.ItemGroupID";
    sqlfunc.GetDataFromDT(dtTemp, qry2, "sqlcon");
    if (dtTemp.Rows.Count > 0)
    {
        for (int i = 0; i < dtTemp.Rows.Count; i++)
        {
            var q5 = from igs in dset.ItemGroupStock.AsEnumerable()
                     where igs.ItemGroupID == Convert.ToInt32(dtTemp.Rows[i][1].ToString())
                     select igs;
            foreach (var res in q5)
            {
                res.OpeningStock -= Convert.ToDecimal(dtTemp.Rows[i][0].ToString());
            }
        }
    }
    //Adding cosumed stock from the entered date.
    dtTemp = new DataTable();
    qry = "select  ig.ItemGroupID, sum(wod.OrderQty) As ConsumptionQty from WineOrder wo, WineOrderDetail wod, ItemGroup ig, Item i where ig.ItemGroupID = i.ItemGroupID AND i.ItemID = wod.ItemID AND wo.IssuedToPersonnelID is not null AND wod.IsOrderCancelled = 0 AND wod.WineOrderId = wo.WineOrderId AND wo.WineOrderDate>='" + Convert.ToDateTime(txtDate.Text.Trim()).ToString("MM/dd/yyyy") + "' group by ig.ItemGroupID";
    sqlfunc.GetDataFromDT(dtTemp, qry, "sqlcon");
    if (dtTemp.Rows.Count > 0)
    {
        for (int i = 0; i < dtTemp.Rows.Count; i++)
        {
            var q5 = from igs in dset.ItemGroupStock.AsEnumerable()
                     where igs.ItemGroupID == Convert.ToInt32(dtTemp.Rows[i][0].ToString())
                     select igs;
            foreach (var res in q5)
            {
                res.OpeningStock += Convert.ToDecimal(dtTemp.Rows[i][1].ToString());
            }
        }
    }
    dset.AcceptChanges();
    var qry10 = from us in dset.UserConsume.AsEnumerable()
                group us by us.ItemGroupID into g
                select new
                {
                    TOTAL = g.Sum(x => x.ConsumptionQty),
                    IG = g.Key
                };
    foreach (var res in qry10)
    {
        var qrry = from igs in dset.ItemGroupStock.AsEnumerable()
                   where igs.ItemGroupID == res.IG
                   select igs;
        foreach (var res2 in qrry)
        {
            res2.ClosingStock = res2.OpeningStock - res.TOTAL;
        }
    }
    ViewState.Add("vsDset", dset);
    rdoc.Load(Server.MapPath("~/Report/WSDayBookRpt.rpt"));
    rdoc.SetDataSource(dset);
    CrystalReportViewer1.DataBind();
    CrystalReportViewer1.ReportSource = rdoc;
    CrystalReportViewer1.RefreshReport();
    Session["CRpt"]= rdoc;
}
}

请帮帮我.. 我尝试过使用会话,但是因为我无法在第二页之后导航,如果我用它来编写导航页面编号,它的工作正常......

谢谢和问候,

更新

{{1}}

1 个答案:

答案 0 :(得分:1)

您的CrystalReportViewer控件可能会丢失PostBack上的ReportDocument。您必须按照以下方式执行此操作:

在页面级别声明ReportDocument对象

ReportDocument rdoc = new ReportDocument();

在Page_Load事件中从Session对象加载ReportDocument

protected void Page_Load(System.Object sender, System.EventArgs e) {
    if (Page.IsPostBack) {
        if (Session.Item("CRpt") != null) {
            rdoc = Session.Item("CRpt");
        }
        CrystalReportViewer1.ReportSource = rdoc;
        CrystalReportViewer1.RefreshReport();
    } else {
        'NOT POSTBACK
        if (Session.Item("CRpt") != null) {
            Session.Remove("CRpt");
        }
        Session.Add("CRpt", rdoc);
    }
}

在btnSubmit上创建并将ReportDocument添加到Session

protected void btnSubmit_Click(object sender, EventArgs e) {

    //Populate Report 
    rdoc.SetDataSource(dset);
    CrystalReportViewer1.ReportSource = rdoc;
    Session.Add("CRpt", rdoc);
}

除此之外,如果您在设置LogOnInfo时需要特定帮助,那么您也可以查看此SO帖子Report asking for database login on setting DataTable as DataSource