Click here to Skip to main content
12,403,391 members (73,225 online)
Rate this:
Please Sign up or sign in to vote.
Hi, what i want to do is to populate a datagridview from a database, but with comparing a string from the database with a date..
Here is what i wanna do:

in the database for example i have a column with the value "19.03.2012" (as a string type, i tried to make the datetime type but no success), and i want to select the values that are greater then for example "10.10.2010" and fill the datagrid with the specified value

here is the code where i call to populate the datagrid (i am aware why this code wont work, it is just so you know what i want to do):
private void PopulateDataGrid()
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM TableExample WHERE Date > '10.10.2010'", conn);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            TableExampleBindingSource.DataSource = table;
            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = TableExampleBindingSource;

So how can i use greater or less then, when i want to compare dates?

Thanks in forward...
Posted 19-Mar-12 12:16pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Bad way!

If you are using SQL Server, use DateTime instead of nvarchar.

By the way:
If you want and insist to use string type for date, then change your format. For example:
"20120123" instead of "01.23.2012"

SELECT * FROM TableExample WHERE [Date] > '20101010'
nltd 22-Dec-15 4:51am
Hi! thank you so much. But my problem is not at SQL. I got date into SQL and i compared them then true. When both string datetime get into SQL, have same format ( dd/MM/yyyy). But i set format datetime in my computer... it was wrong :(.
ex: i have
string date_now = "2015/12/22";
string date_end ="22/12/2015";
how way can i compare them?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is quick and dirty, and I don't recommend it. hah! I'm also making a lot of assumptions here... Try this out for an example:

SELECT CONVERT(DATETIME, '19.03.2010', 103)

This will convert your European date/string to the american format. So then you can do this:

SELECT * FROM TableExample WHERE Date > CONVERT(DATETIME, '19.03.2010', 103)

If the problem is that the date could be in any number of formats, then you might think about running a cleaner once you have the data in the dataTable and then doing a table.Select.

The other option would be to clean the data in SQL and change the interface to only accept certain dates/formats. If you give users the chance to enter crap, they will. Smile | :)
nltd 22-Dec-15 4:59am
thank PopeDarren! Now i only want to compare 2 string, which have format datetime different. :)
And I was a beginner programmers so still have a lot be need to learn. Can you give me a solution ? plz.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 19 Mar 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100