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 / .NET Framework / ADO.NET / March 2004

Tip: Looking for answers? Try searching our database.

SQLDateTime Conversion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott M. - 21 Mar 2004 04:11 GMT
How do I convert a date (mm/dd/yy) entered into a textbox into a SQL Server
DateTime field?

I've tried this:
SqlTypes.SqlDateTime.op_Implicit(editText0.Text)

and this:
SqlTypes.SqlDateTime.op_Implicit(editText0.Text & " 12:00:00.000")

and this:
SqlTypes.SqlDateTime.op_Implicit(CType(editText0.Text, DateTime))

But, in each case, I get an error:

System.InvalidCastException: Specified cast is not valid. at
System.Convert.ToDateTime(Object value) at
System.Data.Common.DateTimeStorage.Set(Int32 record, Object value) at
System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store
<9/10/2003 12:00:00 PM> in EventDate Column. Expected type is DateTime.
William Ryan  eMVP - 21 Mar 2004 04:25 GMT
Scott:  Are you trying to set parameter values to the datetime?  If so, try
cmd.Parameters.Add("@FirstParam", SqlDbType.DateTime).Value =
CType(editText0.Text, DateTime))

or cmd.Parameters("@FirstParam").Value = CType(editText0.Text, DateTime)) if
you've already added the params.
> How do I convert a date (mm/dd/yy) entered into a textbox into a SQL Server
> DateTime field?
[quoted text clipped - 15 lines]
> System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store
> <9/10/2003 12:00:00 PM> in EventDate Column. Expected type is DateTime.
Scott M. - 21 Mar 2004 05:02 GMT
No, I'm just trying to take a date (mm/dd/yy) entered into a textbox and
assign it to a sqldatetime field.

> Scott:  Are you trying to set parameter values to the datetime?  If so, try
> cmd.Parameters.Add("@FirstParam", SqlDbType.DateTime).Value =
[quoted text clipped - 22 lines]
> > System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store
> > <9/10/2003 12:00:00 PM> in EventDate Column. Expected type is DateTime.
William Ryan  eMVP - 21 Mar 2004 05:26 GMT
?  How are you doing this assignment?  How's it getting back to the database
for the assignment?
> No, I'm just trying to take a date (mm/dd/yy) entered into a textbox and
> assign it to a sqldatetime field.
[quoted text clipped - 28 lines]
> store
> > > <9/10/2003 12:00:00 PM> in EventDate Column. Expected type is DateTime.
Scott M. - 22 Mar 2004 18:34 GMT
Ok, I've got you now William.  Thanks.  That did the trick!

> ?  How are you doing this assignment?  How's it getting back to the database
> for the assignment?
[quoted text clipped - 32 lines]
> > > > <9/10/2003 12:00:00 PM> in EventDate Column. Expected type is
> DateTime.
Cor - 21 Mar 2004 10:11 GMT
Hi Scott,

Have a look at this, be aware that I have cut some sentences away, so there
can be errors but you get the idea with this.

AddHandler b(i).Format, AddressOf DBdateTextbox
AddHandler b(i).Parse, AddressOf TextBoxDBdate

I use a table to enum the textboxes so I have to do it only once for all
date textboxes.
(And this is for the European date format)

I hope this helps?

Cor

\\\
Private Sub DBdateTextbox(ByVal sender As Object, _
         ByVal cevent As ConvertEventArgs)
       If cevent.Value Is DBNull.Value Then
           cevent.Value = ""
       Else
           Dim datum As Date
           datum = DirectCast(cevent.Value, Date)
           cevent.Value = datum.ToString("dd - MM - yyyy")
       End If
   End Sub
   Private Sub TextBoxDBdate(ByVal sender As Object, _
    ByVal cevent As ConvertEventArgs)
       If cevent.Value.ToString = "" Then
           cevent.Value = DBNull.Value
       End If
   End Sub
///
Cor - 21 Mar 2004 10:16 GMT
Hi Scott,

Now I see you are not using databinding, this sample is with databinding,
but maybe you can use it anyhow.

Cor

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.