Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

c# - How to transfer this code from ASP.NET to ASP.NET MVC to print crystal reports

I have ASP.NET application and some crystal reports and I need to transfer the following code from ASP.NET and use same functionality in MVC , I am using entity framework.

this is the form.aspx code which i send the parameters value to the other form by using hyperlink and NavigateUrl:

<asp:GridView ID="GridView1"  Width="100%" CssClass="dynamicTable table  table-striped table-bordered table-condensed"  style="color:#000000;background-color:#f4eb3c  " AutoGenerateColumns="false"  runat="server">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>Print</HeaderTemplate>
                    <ItemTemplate>
                        <div style="width:100%"> <asp:HyperLink ID="HyperLink1"  NavigateUrl='<%#"/RPT/WebForm1.aspx?order_id=" +Eval("order number")+"&DEPT ID=" +Eval("DEPTID")+"&Test Id=" +Eval("Test Id")+"&Culture=" +Eval("Culture")%>' runat="server">Print Result</asp:HyperLink></div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>Patient MRN#</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("Patient No") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>Order Number</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("order number") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>Patient Name</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("Patient Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>AGE</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("AGE") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>Request Date</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("Collection Date") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>Report Date</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("Report Date") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                
                 <asp:TemplateField>
                    <HeaderTemplate>Test Name</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("Test_Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField>
                    <HeaderTemplate>Result</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" runat="server" Text='<%# Eval("Result") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField>
                    <HeaderTemplate>Ref.Range From</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Reflow" runat="server" Text='<%# Eval("Low Range") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField>
                    <HeaderTemplate>To</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Reflow" runat="server" Text='<%# Eval("High Range") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField>
                    <HeaderTemplate>Text Range</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Reflow" runat="server" Text='<%# Eval("Text Range") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField>
                    <HeaderTemplate>REQ. FORM NO.</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="req" runat="server" Text='<%# Eval("REQ FORM NUMBER") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate></HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" Visible="false" runat="server" Text='<%# Eval("DEPTID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate></HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="LblTest" Visible="false" runat="server" Text='<%# Eval("Culture") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                
            </Columns>
            <EmptyDataTemplate>No Data Found   -    ?? ??? ?????? ??? ??????</EmptyDataTemplate>
        </asp:GridView>

This is the ASP.NET webform.aspx.cs code :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            TXTORDERID.Text = Request.QueryString["order_number"].ToString();
            TXTDEPTID.Text = Request.QueryString["DEPTID"].ToString();
            TXTTESTID.Text = Request.QueryString["Test_Id"].ToString();
            TXTCULTURE.Text = Request.QueryString["Culture"].ToString();
            ReportDocument reportDocument = new ReportDocument();
            ParameterFields paramFields = new ParameterFields();
            ParameterField paramField = new ParameterField();
            ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
            ParameterDiscreteValue paramDiscreteValue1 = new ParameterDiscreteValue();
            ParameterField parameterField1 = new ParameterField();
            ParameterDiscreteValue parameterDiscreteValue1 = new ParameterDiscreteValue();
            ParameterFields parameterFields = new ParameterFields();


            if (Convert.ToInt32(TXTDEPTID.Text) == 7 && Convert.ToInt32(TXTCULTURE.Text) == 1)
            {
                TXTPOSITIVE.Text = Request.QueryString["Positive"].ToString();
            }
            if (Session["UserCustid"] != null && Convert.ToInt32(Session["UserCustid"]) > 0)
            {
                if (Convert.ToInt32(TXTDEPTID.Text) == 1 || Convert.ToInt32(TXTDEPTID.Text) == 2 || Convert.ToInt32(TXTDEPTID.Text) == 3 || Convert.ToInt32(TXTDEPTID.Text) == 4)
                {

                    paramField.Name = "@ORDER_ID";
                    paramDiscreteValue.Value = TXTORDERID.Text.ToString();
                    paramField.CurrentValues.Add(paramDiscreteValue);
                    paramFields.Add(paramField);

                    paramField = new ParameterField(); // <-- This line is added
                    paramDiscreteValue = new ParameterDiscreteValue();  // <-- This line is added
                    paramField.Name = "@deptid";
                    paramDiscreteValue1.Value = TXTDEPTID.Text.ToString();
                    paramField.CurrentValues.Add(paramDiscreteValue1);
                    paramFields.Add(paramField);

                    CrystalReportViewer1.ParameterFieldInfo = paramFields;
                    CrystalReportViewer1.ReuseParameterValuesOnRefresh = true;
                    CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
                    reportDocument.Load(Server.MapPath("~/RPT/RPT_RESULTS.rpt"));
                    CrystalReportViewer1.ReportSource = reportDocument;
                    reportDocument.SetDatabaseLogon("DBadmin", "1111");
                    var connectionInfo = new ConnectionInfo();
                    connectionInfo.ServerName = "DB";
                    connectionInfo.DatabaseName = "DB";
                    connectionInfo.Password = "1111";
                    connectionInfo.UserID = "DBadmin";
                    connectionInfo.Type = ConnectionInfoType.SQL;
                    connectionInfo.IntegratedSecurity = false;
                    for (int i = 0; i < CrystalReportViewer1.LogOnInfo.Count; i++)
                    {
                        CrystalReportViewer1.LogOnInfo[i].ConnectionInfo = connectionInfo;
                    }
                }

                else if (Convert.ToInt32(TXTDEPTID.Text) == 5)
                {

                    paramField.Name = "@Criteria";
                    paramDiscreteValue.Value = TXTORDERID.Text.ToString();
                    paramField.CurrentValues.Add(paramDiscreteValue);
                    paramFields.Add(paramField);

                    paramField = new ParameterField(); // <-- This line is added
                    paramDiscreteValue = new ParameterDiscreteValue();  // <-- This line is added
                    paramField.Name = "@TESTID";
                    paramDiscreteValue1.Value = TXTDEPTID.Text.ToString();
                    paramField.CurrentValues.Add(paramDiscreteValue1);
                    paramFields.Add(paramField);

                    CrystalReportViewer1.ParameterFieldInfo = paramFields;
                    CrystalReportViewer1.ReuseParameterValuesOnRefresh = true;
                    CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
                    reportDocument.Load(Server.MapPath(&q

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I see you completed everything and you need to pass parameters from View to crystal reports try this solution :

<a href="/RPT/WebForm1.aspx/?order_number=@Model.labCashView.FirstOrDefault().order_number&deptid=@Model.labCashView.FirstOrDefault().DEPTID&testid=@Model.labCashView.FirstOrDefault().Test_Id&Culture=@Model.labCashView.FirstOrDefault().Culture">Print Result</a>

Then in webform.aspx.cs write the following code and use the value passed from the link :

TXTORDERID.Text = Request.QueryString["order_number"].ToString();
            TXTDEPTID.Text = Request.QueryString["deptid"].ToString();
            TXTTESTID.Text = Request.QueryString["testid"].ToString();
            TXTCULTURE.Text = Request.QueryString["Culture"].ToString(); 

Hope this will help you and print crystal reports in your MVC view.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...