Returning a short date in a datareader

  • Thread starter Thread starter axapta
  • Start date Start date
A

axapta

Hi,
I have the following however the date is returned in the long format. I want
to display as dd/mm/yyyy.

If Not drReader.Item("offerstatusdate") Is DBNull.Value Then
txtOfferStatusDate.Text = drReader.Item("offerstatusdate")
End If
Regards
 
Hi,
I have the following however the date is returned in the long format. I want
to display as dd/mm/yyyy.

If Not drReader.Item("offerstatusdate") Is DBNull.Value Then
txtOfferStatusDate.Text = drReader.Item("offerstatusdate")
End If
Regards

if not drReader("offerstatusdate") is dbnull.value then
dim dtTempDate as datetime =
Convert.toDateTime(drReader("offerstatusdate")
txtOfferStatusdate.Text = dtTempdate.ToShortdateTimeString()
end if
 
if not drReader("offerstatusdate") is dbnull.value then
dim dtTempDate as datetime =
Convert.toDateTime(drReader("offerstatusdate")
txtOfferStatusdate.Text = dtTempdate.ToShortdateTimeString()
end if
 
if not drReader("offerstatusdate") is dbnull.value then
dim dtTempDate as datetime =
Convert.toDateTime(drReader("offerstatusdate")
txtOfferStatusdate.Text = dtTempdate.ToShortdateTimeString()
end if

You could also use:

txtOfferStatusdate.Text =
Convert.ToDateTime(drReader("offerstatusdate")).ToShortDateString()

or:

Dim offerStatusDateIndex As Integer = _
drReader.GetOrdinal("offerstatusdate")

If Not drReader.IsDBNull(offerStatusDateIndex) Then
txtOfferStatusdate.Text = _
drReader.GetDateTime(offerStatusDateIndex).ToShortDateString()
 
axapta said:
Hi,
I have the following however the date is returned in the long format.

That's most likely not a completely accurate description of what's
happening.

If the field is a datetime field in the database, it's not returned as a
formatted string, it's returned as a DateTime value. That means that the
actual formatting is done when you assign the value to the text property.
I
want to display as dd/mm/yyyy.

If Not drReader.Item("offerstatusdate") Is DBNull.Value Then
txtOfferStatusDate.Text = drReader.Item("offerstatusdate")
End If

Instead of reading the value as an Object and implicitly converting the
DateTime value into a string, read the value as a DateTime value, and
explicitly format it the way that you want it:

txtOfferStatusDate.Text =
drReader.GetDateTime(drReader.GetOrdinal("offerstatusdate")).ToShortDateString()

or

txtOfferStatusDate.Text =
drReader.GetDateTime(drReader.GetOrdinal("offerstatusdate")).ToString("d")

or

txtOfferStatusDate.Text =
drReader.GetDateTime(drReader.GetOrdinal("offerstatusdate")).ToString("dd/MM/yyyy")

Note:
If you are doing this in a loop, you can call GetOrdinal outside the
loop and store the index of the field in an Integer variable.
 
If the field is a datetime field in the database, it's not returned as a
formatted string, it's returned as a DateTime value. That means that the
actual formatting is done when you assign the value to the text property.
In my idea is that a not completely true, databases where returning kind of
DateTime values long before there was the DotNet DateTime structure.
Therefore the DateTime is represented as a value in an object, but not
direct a DotNet DateTime Value.

I never have investigated what value is returned, but surely not a DotNet
DateTime, which is completely different from the ShortDate and DateTime used
in databases even in its long format, at least before version SQL server
2008.

Cor
 
Cor said:
In my idea is that a not completely true, databases where returning kind
of DateTime values long before there was the DotNet DateTime structure.
Therefore the DateTime is represented as a value in an object, but not
direct a DotNet DateTime Value.

I never have investigated what value is returned, but surely not a
DotNet DateTime, which is completely different from the ShortDate and
DateTime used in databases even in its long format, at least before
version SQL server 2008.

Cor

Nitpicking, aren't we? ;)

It's quite irrelevant what format the database uses to send the data to
the database driver (and it probably varies depending on the database
interface chosen). You can not access the data in that format, you can
only access it as a DateTime value.
 
Göran Andersson said:
Nitpicking, aren't we? ;)

It's quite irrelevant what format the database uses to send the data to
the database driver (and it probably varies depending on the database
interface chosen). You can not access the data in that format, you can
only access it as a DateTime value.
Exactly

:-)

Cor
 
Back
Top