Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / December 2005

Tip: Looking for answers? Try searching our database.

Controlling the date format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Terry - 28 Dec 2005 12:39 GMT
The following line binds a Date from sql Server to a text box control
Me.txtRequestedDate.DataBindings.Add(New Binding("Text", ds,
"tblRequests.RequestDate"))
However, the date returned includes the time. Only the DATE part is
required. The Text box no longer has a Format property so how do you control
the format? Does it need to be controlled earlier when defining the DS?
Paul Clement - 28 Dec 2005 14:13 GMT
¤ The following line binds a Date from sql Server to a text box control
¤ Me.txtRequestedDate.DataBindings.Add(New Binding("Text", ds,
¤ "tblRequests.RequestDate"))
¤ However, the date returned includes the time. Only the DATE part is
¤ required. The Text box no longer has a Format property so how do you control
¤ the format? Does it need to be controlled earlier when defining the DS?
¤

Try using the Transact SQL Convert function.

http://msdn.microsoft.com/library/en-us/tsqlref/ts_ca-co_2f3o.asp?frame=true

Paul
~~~~
Microsoft MVP (Visual Basic)
Terry - 28 Dec 2005 22:11 GMT
Hello Paul
Thank you for the information. The CONVERT function returns the required
format in SQL Server but VB .Net, in its wisdom, still displays the time part
as well. If does, though, display 12:00:00 rather that the actual time in the
record. It seems the convert function does bloack the time part but VB .Net
puts it back in with the default. Close! But no cigar.
I'll check out Jay's comments.
Terry

> ¤ The following line binds a Date from sql Server to a text box control
> ¤ Me.txtRequestedDate.DataBindings.Add(New Binding("Text", ds,
[quoted text clipped - 11 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
Paul Clement - 29 Dec 2005 15:58 GMT
¤ Hello Paul
¤ Thank you for the information. The CONVERT function returns the required
¤ format in SQL Server but VB .Net, in its wisdom, still displays the time part
¤ as well. If does, though, display 12:00:00 rather that the actual time in the
¤ record. It seems the convert function does bloack the time part but VB .Net
¤ puts it back in with the default. Close! But no cigar.
¤ I'll check out Jay's comments.

Hmmm...not sure how that could be happening if you're returning it as text w/o a time value. I guess
I would have to see the syntax of your CONVERT function.

Paul
~~~~
Microsoft MVP (Visual Basic)
Terry - 31 Dec 2005 23:25 GMT
Ooops! Did not read the fine print. Got so excited I used to 103 option
rather than the text.
Thanks Paul

> ¤ Hello Paul
> ¤ Thank you for the information. The CONVERT function returns the required
[quoted text clipped - 10 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
Jay B. Harlow [MVP - Outlook] - 28 Dec 2005 17:17 GMT
Terry,
| The Text box no longer has a Format property so how do you control
| the format?
In .NET 1.0 & 1.1 (VS 2002 & 2003) Handle the Format & Parse events of the
Binding object itself.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemWindowsFormsBindingClassFormatTopic.asp


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemWindowsFormsBindingClassParseTopic.asp


Something like:

   Const format As String = "d"

   Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
       MyBase.OnLoad(e)
       Dim ds As DataSet = ...

       Dim RequestedDateBinding As New Binding("Text", ds,
"tblRequests.RequestDate")

       AddHandler RequestedDateBinding.Format, AddressOf DateFormat
       AddHandler RequestedDateBinding.Parse, AddressOf DateParse

       Me.txtRequestedDate.DataBindings.Add(RequestedDateBinding)

   End Sub

   Private Sub DateFormat(ByVal sender As Object, ByVal e As
ConvertEventArgs)
       If e.DesiredType Is GetType(String) AndAlso TypeOf e.Value Is
DateTime Then
           e.Value = DirectCast(e.Value, DateTime).ToString(format)
       End If
   End Sub

   Private Sub DateParse(ByVal sender As Object, ByVal e As
ConvertEventArgs)
       If e.DesiredType Is GetType(DateTime) AndAlso TypeOf e.Value Is
String Then
           e.Value = DateTime.ParseExact(DirectCast(e.Value, String),
format, Nothing)
       End If
   End Sub

I would consider inheriting from either TextBox or Binding (or possibly
both) to encapsulate the above logic. For example add a FormatString &
FormatProvider to Binder much like .NET 2.0 (VS 2005) does.

In .NET 2.0 adds properties to the Binding object to support formatting,
simplifying the above code:

http://msdn2.microsoft.com/b5awfas2(en-US,VS.80).aspx
http://msdn2.microsoft.com/fe4za7tx(en-US,VS.80).aspx
http://msdn2.microsoft.com/fs5t4etx(en-US,VS.80).aspx

       Dim RequestedDateBinding As New Binding("Text", ds,
"tblRequests.RequestDate")
       RequestedDateBinding.FormattingEnabled = True
       RequestedDateBinding.FormatString = format
       Me.txtRequestedDate.DataBindings.Add(RequestedDateBinding)

Signature

Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

| The following line binds a Date from sql Server to a text box control
| Me.txtRequestedDate.DataBindings.Add(New Binding("Text", ds,
| "tblRequests.RequestDate"))
| However, the date returned includes the time. Only the DATE part is
| required. The Text box no longer has a Format property so how do you control
| the format? Does it need to be controlled earlier when defining the DS?
Terry - 28 Dec 2005 22:27 GMT
Thanks Jay
Wow! This really is a step forward from Format(Date,"Short Date") or setting
the Textbox format.
Terry

> Terry,
> | The Text box no longer has a Format property so how do you control
[quoted text clipped - 65 lines]
> control
> | the format? Does it need to be controlled earlier when defining the DS?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.