简体   繁体   中英

Passing Textbox value to Crystal Report Parameter c#

Could someone please help with my problem. I already have the codes for this but I can't seem to combine the two. Legend: dt1 = Dataset; crpt = Crystal Report File

This is my code for loading the database values into the Crystal Report filtered with the value of the txtGender textbox:

            SqlConnection conn = conString.getCon();
            ReportDocument cy = new ReportDocument();
            dt1 ds = new dt1();
            conn.Open();
            cy.Load(Application.StartupPath + @"\crpt.rpt");



            SqlDataAdapter da = new SqlDataAdapter("exec viewInfo @gen", conn);
            da.SelectCommand.Parameters.AddWithValue("@gen", txtGender.Text);


            da.Fill(ds.Info);
            cy.SetDataSource(ds);

            crystalReportViewer1.ReportSource = cy;

            conn.Close();

While this is my code for Passing the textbox txtGender value into the Crystal Report Parameter object named "Gen":

        ParameterFields pField = new ParameterFields();
        ParameterField pTitle = new ParameterField();
        ParameterDiscreteValue pValue = new ParameterDiscreteValue();

        pTitle.ParameterFieldName = "Gen"; //the name of the field @ Crystal Report

        pValue.Value = txtGender.Text; //sending the text box value
        pTitle.CurrentValues.Add(pValue);
        pField.Add(pTitle);

        crystalReportViewer1.ParameterFieldInfo = pField;


        crpt objBT = new crpt();
        objBT.Refresh();            

        crystalReportViewer1.ReportSource = objBT;

My problem is how do I combine these two codes so that when I enter the value in the textbox it will load the database values into the Crystal Report and pass the value of the textbox into the Crystal Report Parameter Object "Gen". I have tried combining these codes but it prompts a message "Parameter value is incorrect". Anyone?

I hope the following reference link will help for your problem.

C# Crystal Reports String parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM