Click here to Skip to main content
11,644,286 members (69,366 online)
Rate this: bad
good
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;
            da.Fill(table);
            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
good
Please Sign up or sign in to vote.

Solution 2

Hello

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'
  Permalink  
v4
Rate this: bad
good
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 | :)
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 650
1 jyo.net 494
2 Afzaal Ahmad Zeeshan 423
3 Sergey Alexandrovich Kryukov 409
4 CPallini 352
0 OriginalGriff 925
1 DamithSL 636
2 Afzaal Ahmad Zeeshan 622
3 Sergey Alexandrovich Kryukov 548
4 CPallini 521


Advertise | Privacy | Mobile
Web04 | 2.8.150731.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