Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I've crystal report formal problem .I took the code from this website:
http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-date-to-date.htm[^]
,but when I do the formal it gave error string required.

[edit]Additional information copied from comment below:[/edit]
Also, I found this code and did some changes on it ,but the problem is it gives me all the date?
C#
private void button1_Click(object sender, EventArgs e)
        {
            string from_date = textBox1.Text ;
            string to_date = textBox2.Text;
            SqlConnection con=new SqlConnection (@"Data Source=.\Ahmed;Initial Catalog=Market;Integrated Security=True");
            String data = "SELECT * from Product  where dat>='"+from_date+"' and dat<='"+to_date+"'";
            con.Open();
            SqlDataAdapter ad1 = new SqlDataAdapter(data, con);
            DataSet ds1 = new DataSet();
            ad1.Fill(ds1);
            ReportDocument cr  = new ReportDocument();
            cr.Load(@"C:\Users\Ahmed\Documents\Visual Studio 2010\Projects\WindowsFormsApplication9\WindowsFormsApplication9\CrystalReport4.rpt");
 
            cr.SetDataSource(ds1.Tables["product"]);
            crystalReportViewer1.ReportSource = cr;
 
        }
Posted
Updated 1-Nov-12 7:43am
v2
Comments
Member 8584763 1-Nov-12 13:26pm    
Also, I found this code and did some changes on it ,but the problem is it gives me all the date?
private void button1_Click(object sender, EventArgs e)
{
string from_date = textBox1.Text ;
string to_date = textBox2.Text;
SqlConnection con=new SqlConnection (@"Data Source=.\Ahmed;Initial Catalog=Market;Integrated Security=True");
String data = "SELECT * from Product where dat>='"+from_date+"' and dat<='"+to_date+"'";
con.Open();
SqlDataAdapter ad1 = new SqlDataAdapter(data, con);
DataSet ds1 = new DataSet();
ad1.Fill(ds1);
ReportDocument cr = new ReportDocument();
cr.Load(@"C:\Users\Ahmed\Documents\Visual Studio 2010\Projects\WindowsFormsApplication9\WindowsFormsApplication9\CrystalReport4.rpt");

cr.SetDataSource(ds1.Tables["product"]);
crystalReportViewer1.ReportSource = cr;

}
Sushil Mate 1-Nov-12 13:46pm    
put the exception/error?
Member 8584763 1-Nov-12 13:52pm    
Well, I think the problem in the strings it doesn't recognize them. Because dat type is date.
Sushil Mate 1-Nov-12 13:53pm    
so you can convert the date type to string.
Member 8584763 1-Nov-12 13:56pm    
how?

1 solution

The problem is converting the string values to dates. To accomplish this you should use an actual SqlCommand and SqlParameters (to prevent SQL injection). You are also going to need to convert the values the user entered in a the text box to DateTime objects. You should also use Between in your WHERE clause instead of <>.

Your code should look similar to this:
*** Note: I just typed this into the browser so it probably wont compile ***
C#
private void button1_Click(object sender, EventArgs e)
        {
            string from_date = textBox1.Text ;
            string to_date = textBox2.Text;
            DataSet ds1 = new DataSet();
            ReportDocument cr  = new ReportDocument();

            using (SqlConnection con=new SqlConnection (@"Data Source=.\Ahmed;Initial Catalog=Market;Integrated Security=True")){
                String data = "SELECT * from Product  where dat BETWEEN @StartDate AND @EndDate";
                con.Open();
                using (SqlCommand cmd = new SqlCommand(data, conn))
                {
                    // you are going to need to parse these dates in their expected format
                    // Convert.ToDateTime has some overrides for that
                    // See below for some links for some reading
                    cmd.Parameters.Add(new SqlParameter("@StartDate", Convert.ToDateTime(from_date));
                   cmd.Parameters.Add(new SqlParameter("@EndDate", Convert.ToDateTime(to_date));
                   using (SqlDataAdapter ad1 = new SqlDataAdapter(cmd))
                   {
                      ad1.Fill(ds1);
                   }
                }
            }

            cr.Load(@"C:\Users\Ahmed\Documents\Visual Studio 2010\Projects\WindowsFormsApplication9\WindowsFormsApplication9\CrystalReport4.rpt");
            
            cr.SetDataSource(ds1.Tables["product"]);
            crystalReportViewer1.ReportSource = cr;
 
        }


You should probably read up on this stuff so here are some links:
SQL Between Operator[^]
SqlCommand Class[^]
SqlParameter Class[^]
C# using Statement[^]
DateTime.TryParseExact[^]
Convert.ToDateTime[^]
 
Share this answer
 
v2
Comments
Member 8584763 2-Nov-12 7:40am    
still the same problem it gives me all dates?

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