Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on code in C# where I used OLEDB connection string to connect MS access database. I have a form where I show data from database in datagridview on some criteria. Below are criteria:

a) Person (come from database in)

b) Process (come from database in textbox)

c) From Date (Datetimepicker)

d) To Date (Datetimepicker)

Result what I want: first I select person and than process and than From Date and then To date and click on View Button. which should show data from MS-Access based on above criteria I selected.

What I have tried:

1. For Person and Process filter:
DataView DV = new DataView(dt1);
        DV.RowFilter = string.Format("[Person] LIKE '%{0}%'", textBox5.Text);
        dataGridView1.DataSource = DV;

For Date Time between Two date I tried many and Google lots but not find answer. I tried below:
a)
SqlConnection con = new 
        SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        string queryString = "";
        queryString = "SELECT * FROM Table1 WHERE dob BETWEEN @startdate AND @enddate";
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(queryString, con);
        sqlCmd.Parameters.Add("@startdate", System.Data.SqlDbType.Date).Value = textBox7.Text;
        sqlCmd.Parameters.Add("@enddate", System.Data.SqlDbType.Date).Value = textBox8.Text;
        System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(dataSet);
        GridView1.DataSource = dataSet;
        GridView1.DataBind();
b)

        string FD = "";
        FD = dateTimePicker4.Value.ToString("dd-MM-yyyy");
        string TD = "";
        TD = dateTimePicker5.Value.ToString("dd-MM-yyyy");
        connection.Close();
        connection.Open();
        OleDbCommand command123 = new OleDbCommand();
        command123.Connection = connection;
        string query123 = "select * from Table1 where [P Date] between date '"# + dateTimePicker4.Text.ToString() + #"' and date '"# + dateTimePicker5.Text.ToString() + #"'"
        command123.CommandText = query123;
        OleDbDataAdapter da123 = new OleDbDataAdapter(command123);
        DataTable dt123 = new DataTable();
        da123.Fill(dt123);
        dataGridView1.DataSource = dt123;
c)

        DataTable dt = new DataTable();
        da.Fill(dt);
        DataView DV = new DataView(dt1);
        DV.RowFilter = string.Format("[P Date] >=" + textBox7.Text + " and <" + textBox8.Text + "");
        dataGridView1.DataSource = DV;
d)

        DataTable dt1 = new DataTable();
        DataView DV = new DataView(dt1);
        //DV.RowFilter = "[P Date] IN (#11/01/2019#, #11/11/2019#)";
        //DV.RowFilter = "[P Date] >=#"+dateTimePicker4.Text+"# and [P Date] <=#"+dateTimePicker5.Text+"#";
        //dataView.RowFilter = "Date IN (#12/31/2008#, #1/1/2009#)" // date time values
        //DV.RowFilter ="([P Date] >=CDate('dateTimePicker4.Text')) and ([P Date] <=CDate('dateTimePicker5.Text'))";
        //DV.RowFilter = string.Format(CultureInfo.InvariantCulture.DateTimeFormat, "([P Date]>=#{dateTimePicker4.text}#) and ([P Date] <=#{dateTimePicker5.Text}#)");
        dataGridView1.DataSource = DV;

For connection I am trying OLEDB connection.
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabasePath.accdb;
    Jet OLEDB:Database Password=password";
connection.Close();
connection.Open();
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
//Select all column use belw query
string query = "select * from Table1";
command1.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command1);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();


this code is working but problem is that it shows all data not based on criteria based data.

I expect the output in Datagridview based on all criteria. Show only data which falls under all criteria.
Posted
Updated 12-Nov-19 21:41pm
v2

Instead of IN, use BETWEEN:
SQL
DateColumn BETWEEN startDate AND endDate

And more importantly: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Try to format the date values like this:
Where [P Date] Between #" + dateTimePicker4.Value.ToString("yyyy'/'MM'/'dd") + "# And #" + dateTimePicker5.Value.ToString("yyyy'/'MM'/'dd") + "#"
 
Share this answer
 
Comments
Member 13132705 13-Nov-19 8:03am    
for dates code is working fine now. But when i try it with Person and process it is not working. I mean I want data in datagridview based on person wise --> his respective process--> from date--> to date.
from and to date is working fine.

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