DateTime in C# - GetDate in SQL - A small confusion

.NET programming topics
Post Reply
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

DateTime in C# - GetDate in SQL - A small confusion

Post by Neofriend » Sat Jan 30, 2010 2:13 am

I have a not null column in table where its saving current date and time using getDate function in SQL by default.

Now I'd like to generate reports. Am using MicrosoftReportViewer.

Would like to give a DateTimePicker, where the person can select the year, date or change any perspective of date. It should check the getDate() of that column in database and update the report based on that date.

Is that easily implementable? - As I have very short span of time left.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: DateTime in C# - GetDate in SQL - A small confusion

Post by Neo » Sun Jan 31, 2010 3:28 pm

Add your date/time field as a Report Parameter and do something as below.

VB.Net code

Code: Select all

    Dim params(0) As Microsoft.Reporting.WebForms.ReportParameter

    Dim p As Microsoft.Reporting.WebForms.ReportParameter
    p = New Microsoft.Reporting.WebForms.ReportParameter("YourField", dtpicker1.value)

    params(0) = p

    ReportViewer1.ServerReport.SetParameters(params)

    ReportViewer1.ServerReport.Refresh()
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: DateTime in C# - GetDate in SQL - A small confusion

Post by Neofriend » Sun Jan 31, 2010 4:11 pm

in C# :)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: DateTime in C# - GetDate in SQL - A small confusion

Post by Neo » Sun Jan 31, 2010 4:51 pm

Find a good example in C# here.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: DateTime in C# - GetDate in SQL - A small confusion

Post by Neofriend » Tue Feb 02, 2010 3:17 am

Just a small question about DateTime - I have smalldatetime stored in sql database table.

Now I'd like to compare the month part of DateTimePicker with the databased stored date. How to compare the month of DateTimePicker with that of database table value.

Please help asap.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: DateTime in C# - GetDate in SQL - A small confusion

Post by Neofriend » Tue Feb 02, 2010 4:08 am

Let me ask it if something with substr is possible. I am okay if I can add a column to the table using substr and get only the month part of it.

But am not able to successfully achieve this. Need help.

Thank you.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: DateTime in C# - GetDate in SQL - A small confusion

Post by Neo » Tue Feb 02, 2010 2:21 pm

SELECT * FROM table1 WHERE MONTH(field1) = dtpicker1.value.month()

Similarly you could use, DAY, YEAR to get day or year part of the field.

If you need to get more parts of date/time, you can use DATEPART function as below.
SQL Syntax: DATEPART ( datepart , date field )

To get month, we need to use 'm' abbreviations as below.
SELECT * FROM table1 WHERE DATEPART (m, field1) = dtpicker1.value.month()

Instead of m, you can use several datepart abbreviations as below.
datepartAbbreviations
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
weekdaydw
hourhh
minutemi, n
secondss, s
millisecondms
microsecondmcs
nanosecondns
TZoffsettz
ISO_WEEKisowk, isoww
Post Reply

Return to “.NET Programming”