Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys! i have two date pickers. and i want to fill data grid between selected dates from date pickers. my code is not working.

What I have tried:

C#
OleDbCommand cmd = new OleDbCommand("SELECT * FROM invoice where DateTime between '" + dateTimePicker1.Value.ToString() + "' AND '" + dateTimePicker2.Value.ToString() + "'",con);
            con.Open();
            OleDbDataReader sdr = cmd.ExecuteReader();
            if ((sdr.Read() == true))
            {
                DataSet ds = new DataSet();
                da.Fill(ds, "invoice");
                dataGridView1.DataSource = ds.Tables["invoice"];
                //dataGridView1.DataSource = sdr;

            }
            else
            {
                MessageBox.Show("query not executed");
            }
Posted
Updated 28-Oct-17 19:13pm
v2
Comments
CHill60 12-Jun-16 7:26am    
"not working" does not give us enough information to help you. What is happening?
Your code is also vulnerable to SQL injection - do not use string concatenation to generate SQL queries. Use Parameterised queries instead - you may also find that your problem goes away as a result

Try this :
C#
OleDbCommand cmd = new OleDbCommand("SELECT * FROM invoice where DateTime between '" + dateTimePicker1.Value.ToString() + "' AND '" + dateTimePicker2.Value.ToString() + "'",con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "invoice");
dataGridView1.DataSource = ds.Tables["invoice"];
 
Share this answer
 
Comments
CHill60 12-Jun-16 7:36am    
This code is still vulnerable to SQL Injection attack.
R45H1D 12-Jun-16 7:37am    
how to prevent it from sql injection?
R45H1D 12-Jun-16 7:37am    
bro above code gives me this error. on this line (da.Fill(ds, "invoice");)
Object reference not set to an instance of an object.
Mehdi Gholam 12-Jun-16 7:41am    
I presumed your code worked, make sure "da" SQLDataAdapter is initialized first.
R45H1D 12-Jun-16 7:45am    
now this error on same line.
The SelectCommand property has not been initialized before calling 'Fill'.
This works
C#
string connectionstring = Settings.Default.SandboxConnectionString;
using (OleDbConnection con = new OleDbConnection(connectionstring))
{
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM invoice where DateTime between @date1 AND @date2", con);
    cmd.Parameters.AddWithValue("@date1", dateTimePicker1.Value);
    cmd.Parameters.AddWithValue("@date2", dateTimePicker2.Value);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);

    con.Open();
    DataSet ds = new DataSet();
    da.Fill(ds, "invoice");
    dataGridView1.DataSource = ds.Tables["invoice"];
}

But only if there is actually data on the table that matches the dates I selected.
Things to note:
1. I've used a parameterised query.
2. There is no .ToString() on the dateTimePicker values
3. You should really enumerate the columns you want returned from the table i.e. use
SQL
SELECT col1, col2, ..., colN FROM invoice
instead of
SQL
SELECT * from invoice

4. You should avoid using reserved words for column names on tables - use InvoiceDate rather than DateTime for example
 
Share this answer
 
Comments
R45H1D 13-Jun-16 1:52am    
when i run this code, it show this error on con.open() line...
The ConnectionString property has not been initialized
CHill60 13-Jun-16 4:17am    
You have to substitute your own connection string - you had a connection con in your original post - use that.
R45H1D 13-Jun-16 2:02am    
this code is not working bro. it displays only column names. but not rows
CHill60 13-Jun-16 4:16am    
In that case you do not have any invoices in your table that match the dates you are entering. If it "wasn't working" at all then you would not get the column names either.
C#


C#
OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM invoice where DateTime between '" +
            dateTimePicker1.Value.ToShortDateString() + "' AND '" + dateTimePicker2.Value.ToShortDateString() +
                "'", con);
           
            DataSet ds = new DataSet();

            da.SelectCommand = cmd;
            con.Open();
            da.Fill(ds, "invoice");
            dataGridView1.DataSource = ds.Tables["invoice"];
            con.Close();
        }
 
Share this answer
 
Comments
Member 12654684 26-Jul-16 11:36am    
solution 2 works perfect

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