SQLDateTime Conversion

S

Scott M.

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.
 
W

William Ryan eMVP

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.
 
S

Scott M.

No, I'm just trying to take a date (mm/dd/yy) entered into a textbox and
assign it to a sqldatetime field.
 
W

William Ryan eMVP

? How are you doing this assignment? How's it getting back to the database
for the assignment?
 
C

Cor

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
///
 
C

Cor

Hi Scott,

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

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top