Click here to Skip to main content
11,934,783 members (50,207 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 13: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'
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 | :)

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
Web03 | 2.8.151126.1 | Last Updated 19 Mar 2012
Copyright © CodeProject, 1999-2015
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