Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear ALL,

i am making userform in C# and using MS access database i want know how can be add date parameter in report viewer in to generate report from and to date plz help me....................................................................................................

What I have tried:

visual studio 2010................................................................................................................
Posted
Updated 28-Feb-18 23:09pm
Comments
OriginalGriff 1-Mar-18 5:03am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
What code have you tried?
Where are you stuck?
What help do you need?
Use the "Improve question" widget to edit your question and provide better information.

1 solution

You don't need to pass date parameters to report viewer , instead use those parameters as filter in your SQL query to filter the date and then populate data table for that output
finally attach that data table as data source to your report viewer

here is the sample code I tried
Date1 and Date2 are parameter object (text box or calendar objects on form)

private void Form1_Load(object sender, EventArgs e)
{
    Customers dsCustomers = GetData();
    ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
    this.reportViewer1.LocalReport.DataSources.Clear();
    this.reportViewer1.LocalReport.DataSources.Add(datasource);
    this.reportViewer1.RefreshReport();
}
 
private Customers GetData()
{
    string constr = @"Data Source=.\Sql2005;Initial Catalog=Northwind;Integrated Security = true";
    using (SqlConnection con = new SqlConnection(constr))
    {
        // NO! Bad idea. Google "SQL Injection".
        // using (SqlCommand cmd = new SqlCommand("SELECT TOP 20 * FROM customers where Transdate between" & Date1.value & " AND " & Date2.value))
       
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 20 * FROM customers where Transdate between @Date1 AND @Date2"))
        {
            cmd.Parameters.AddWithValue("@Date1", Date1.Value);
            cmd.Parameters.AddWithValue("@Date2", Date2.Value);
            
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (Customers dsCustomers = new Customers())
                {
                    sda.Fill(dsCustomers, "DataTable1");
                    return dsCustomers;
                }
            }
        }
    }
}
 
Share this answer
 
v2
Comments
CHill60 1-Mar-18 5:11am    
Don't use string concatentation to form SQL queries! Use Parameterized queries instead. It will also help you with the missing single-quotes in your query.
RDBurmon 1-Mar-18 5:33am    
agree, it will prevent SQL injection
Richard Deeming 1-Mar-18 13:26pm    
It will also fix the syntax error you currently have in your query. :)

(Also the incorrect attempt to use VB.NET syntax in C#.)

I've fixed your code for you.
RDBurmon 5-Mar-18 1:06am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900