Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
C#
Hello Guys! i want to know how to select data from ms access database in c# from today date i can select data from database between date but when i select today date and  get data it;s show me blank. so please help me.Here is my Code!

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Today Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] = #" + dateTimePicker1.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else if (textBox1.Text == "Custom Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] between #" + dateTimePicker2.Value.ToString() + "# AND #" + dateTimePicker3.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else
            {
                MessageBox.Show("Sorry! File was not Found:Please Try Again", "Daily Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



What I have tried:

C#
Hello Guys! i want to know how to select data from ms access database in c# from today date i can select data from database between date but when i select today date and  get data it;s show me blank. so please help me.Here is my Code!

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Today Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] = #" + dateTimePicker1.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else if (textBox1.Text == "Custom Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] between #" + dateTimePicker2.Value.ToString() + "# AND #" + dateTimePicker3.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else
            {
                MessageBox.Show("Sorry! File was not Found:Please Try Again", "Daily Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

Posted
Updated 25-Jun-16 3:08am

Google for "C# Access database SQL parameterized queries" to find out why what you're doing is so bad and what to do about it.

You can find out more by Googling for "SQL Injection Attack".
 
Share this answer
 
v2
Comments
Gustav Brock 25-Jun-16 9:02am    
While your comment is right, it is not an answer.
Dave Kreskowiak 25-Jun-16 11:03am    
Yes, it is. I prefer to point in the right direction over just giving an answer where the OP had no idea how's to get to it.

How long have you been around here again?
Gustav Brock 25-Jun-16 11:25am    
Again?
Everyone can toss a link, and the questioneer certainly has an idea, only just misses the final detail.
"
Quote:
i can select data from database between date
So, you can re-define your goal as selecting data based on midnight of the current day until midnight of then next day:
C#
private TimeSpan private TimeSpan OneTick = TimeSpan.FromTicks(1L);
             
public DateTime StartOfToday()
{
    return DateTime.Today;
}

public DateTime EndOfToday()
{
    return DateTime.Today.AddDays(1).Subtract(OneTick);
}
 
Share this answer
 
You may need the correct format for the string expressions of the date values:

C#
" .. 
Where [Date] Between #" + dateTimePicker2.Value.ToString("yyyy'/'MM'/'dd") + "# And #" + dateTimePicker3.Value.ToString("yyyy'/'MM'/'dd") + "#", ..
 
Share this answer
 
Comments
Dave Kreskowiak 25-Jun-16 11:05am    
This answer just compounds the problem and opens his database up to SQL Injection attacks and the possibility of complete loss of the database. Maybe YOU should Goggle what I posted above.
Gustav Brock 25-Jun-16 11:31am    
I don't disagree with you, but sometimes a simple question needs a simple answer.
Just curious, but how would you inject a DateTimePicker control? Or rather: make it return anything but a DateTime value?

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