Search code examples
c#winformsparameterscomboboxreportviewer2008

How to display my combobox value onto my report?


I have a form with a combobox and datetimepicker. i want the user to select a value from the combobox and choose a date and this value needs to be captured as a parameter and display on the report where the date equals chosen date and itempiececode equals chosen combobox value.

The Pieces Involved

The Form - Piece Code.cs The Report - Report6.rdlc The ReportViewer on the Form Piece Code.cs - reportviewer1

If you still dont understand what my question is please click the -> Picture

the highlighted yellow is what i need.

The Code i have sooo far

To fill the reportviewer

private void PieceCode_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet9.CandyPieceSize' table. You can move, or remove it, as needed.
    this.CandyPieceSizeTableAdapter.Fill(this.CorsicanaNetWeightDataSet9.CandyPieceSize);
    // TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet4.PieceDimensionMasterDataUpdate' table. You can move, or remove it, as needed.
    this.PieceDimensionMasterDataUpdateTableAdapter.Fill(this.CorsicanaNetWeightDataSet4.PieceDimensionMasterDataUpdate);
    // TODO: This line of code loads data into the 'corsicanaNetWeightDataSet4piece.Item_Piece' table. You can move, or remove it, as needed.
    this.item_PieceTableAdapter.Fill(this.corsicanaNetWeightDataSet4piece.Item_Piece);
    PieceReport pr = new PieceReport();
    reportViewer1.Visible = false;
    this.reportViewer1.RefreshReport();
}

To Fill the Combobox i used datasource and displaymemeber property ** I used the report wizard to generate report6**

i used GUI for most of the program so not much code to share

Summary of what i need i need the combobox1.selectedtext set equal to a parameter on report6 and the datetimepicker.text = to another parameter so i can display it in my reportviewer?? or if there is any other workaround for the program.

I am being completely honest here and please dont close this as off topic, or dock me down because i have provided everything i have for your understanding and please help me as this is the last part of my project and i would really love to complete it before the holidays and spend time with family thanks for all your help

Tried it with Code, GUI just does not cut it at times

namespace CorsicanaNetWeightProgram
{
    public partial class PieceCode : Form
    {

        public string constr = "Data Source=KCMJF1XTR1\\SQLEXPRESS;Initial Catalog=CorsicanaNetWeight;Integrated Security=True";

        public reportfiller fullpiecedetails;

        public piecedescription piece;


        PieceReport pr = new PieceReport(); 
        public PieceCode()
        {
            InitializeComponent();
        }

        private void PieceCode_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet9.CandyPieceSize' table. You can move, or remove it, as needed.

            // TODO: This line of code loads data into the 'corsicanaNetWeightDataSet4piece.Item_Piece' table. You can move, or remove it, as needed.
           // this.item_PieceTableAdapter.Fill(this.corsicanaNetWeightDataSet4piece.Item_Piece);
            PieceReport pr = new PieceReport();
            reportViewer1.Visible = false;
            piece = new piecedescription();
            fillpiece();



            this.reportViewer1.RefreshReport();
        }

        private void fillpiece()
        {
            try
            {
            using(MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
            {
                connection.Open ();
                using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT Item + '-' + Description AS PieceDescription FROM dbo.[Piece Dimension Master Data]" ,connection))
                {
                  MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
                        myadapter.SelectCommand = command;
                        myadapter.Fill(piece, "DataTable1");
                comboBox1.DataSource = piece.DataTable1;
                comboBox1.ValueMember = "PieceDescription";
                comboBox1.DisplayMember = "PieceDescription";


        }
            }
            }

            catch (Exception) { /*Handle error*/ }
            }


        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {          

        }

        private void button1_Click(object sender, EventArgs e)
        { 


            reportViewer1.Visible = true;
            {
                using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
                {
                    fullpiecedetails = new reportfiller();
                    connection.Open();
                    //MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT * From EtimePunchDetail WHERE (EmpID = @empid) And  (Paygroup = @paygroup) And (TransDate >= @fromdate) And (TransDate <= @todate)", connection);
                    MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT * FROM dbo.[Piece Dimension Master Data] WHERE Item + '-' + Description  = @piecedesc", connection);
                    {

                        MSSQL.SqlParameter parmEmp = new MSSQL.SqlParameter();
                        parmEmp.ParameterName = "@piecedesc";
                        parmEmp.Value = comboBox1.SelectedValue;
                        command.Parameters.Add(parmEmp);



                        MSSQL.SqlDataAdapter EtimePunchDetailTableAdapter = new System.Data.SqlClient.SqlDataAdapter();
                        EtimePunchDetailTableAdapter.SelectCommand = command;
                        EtimePunchDetailTableAdapter.Fill(fullpiecedetails, "DataTable1");



                        string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
                        string reportPath = exeFolder + @"\Report6.rdlc";



                        reportViewer1.LocalReport.ReportPath = reportPath;

                        reportViewer1.LocalReport.DataSources.Clear();

                        reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("reportfiller_DataTable1", fullpiecedetails));
                        reportViewer1.RefreshReport();
                        reportViewer1.Refresh();
                        reportViewer1.Visible = true;
                   }
                }
            }

        }

I am getting the following error "Value does not fall within the expected range." At this part of the code reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("reportfiller_DataTable1", fullpiecedetails));


Solution

  • I created a new form, added a fresh report, new data set and did everything through the GUI - Used the wizard to generate the report.

    I edited the data set to the following code

    SELECT Item, Description, [Piece Category], Shape, [Deposit Weight], [Center Weight], [Center Weight Constant], [Coating Weight], [Coating Weight Constant], [Decorations Weight], [Inclusions Weight], [Center Diameter_LL], [Center Diameter_UL], [Center Length_LL], [Center Length_UL], [Center Width_LL], [Center Width_UL], [Center Height_LL], [Center Height_UL], [Center Weight Variation], [Coated Diameter_LL], [Coated Diameter_UL], [Coated Length_LL], [Coated Length_UL], [Coated Width_LL], [Coated Width_UL], [Coated Height_LL], [Coated Height_UL], [Coated Weight Variation], Item + '-' + Description AS [PieceDescription] FROM [Piece Dimension Master Data] WHERE **Item + '-' + Description  = @piece**
    

    Then added a table to the report with all required columns

    The GUI Had Generated this code for me

    namespace CorsicanaNetWeightProgram
    {
        public partial class PieceCodepc : Form
        {
            public PieceCodepc()
            {
                InitializeComponent();
            }
    
            private void PieceCodepc_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'dataSet1.DataTable1' table. You can move, or remove it, as needed.
                this.dataTable1TableAdapter.Fill(this.dataSet1.DataTable1);
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                // TODO: This line of code loads data into the 'empty.Piece_Dimension_Master_Data' table. You can move, or remove it, as needed.
                this.Piece_Dimension_Master_DataTableAdapter.Fill(this.empty.Piece_Dimension_Master_Data, comboBox1.SelectedValue.ToString());
    
                this.reportViewer1.RefreshReport();
    
    
            }
        }
    }
    

    All i did was pass comboBox1.SelectedValue.ToString() to DataTableAdapter and IT WORKED!!!!!