Avatar of Member_2_1242703
Member_2_1242703
 asked on

Updating date/time from VB.NET to SQL

I want to take a value from a textbox in a date format (i.e. mm/dd/yyyy) and update a value in an SQL table with a date/time datatype. Can someone show me an example?
.NET ProgrammingVisual Basic.NETASP.NET

Avatar of undefined
Last Comment
Member_2_1242703

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rick

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
xrok

Update meaning SQL already has Date/Time data?
you just want a update Date only?
Member_2_1242703

ASKER
xrok, correct. There is a value already in the table and I just want to update the date
xrok

You need pull data from SQL  then replace date with textbox.
using same time
Your help has saved me hundreds of hours of internet surfing.
fblack61
xrok

Basic
dim sqldate as datetime = sqldatetime
dim textdate as datetime = textdate
newdate = textdate.ToShortDateString & " " & sqldate.ToShortTimeString
Member_2_1242703

ASKER
rick...

With the value 01/02/2012 your code returns

Conversion from string "2012-00-02 00:00:00.000" to type Date is not valid

Why is 01 coming across as 00??
Coralie Crumrine

Try this:

dim cmdText as String

cmdText = "UPDATE dbo.tblTableName SET DateColumnName=CONVERT(datetime, '" & txtbox.year & "/" & txtbox.month & "/" & txtbox.day & " 00:00:00" & "')"

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rick

mwmiller,

What do you mean?

   textbox.text = "01/02/2012"
   cdate(textbox1.text).tostring("yyyy-MM-dd hh:mm:ss")

This gives you: 2012-01-02 12:00:00
Is this not the format you need?



You could also use:

   cdate(textbox1.text).tostring("yyyy-MM-dd hh:mm:ss")

This will give you: 2012-01-02 12:00:00.000
Rick

*
You could also use:

   cdate(textbox1.text).tostring("yyyy-MM-dd hh:mm:ss.fff")

This will give you: 2012-01-02 12:00:00.000
Member_2_1242703

ASKER
Look at the error...

Conversion from string "2012-00-02 00:00:00.000" to type Date is not valid
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Rick

Could you post your code?
Member_2_1242703

ASKER
I've tried several different ways, this is the latest. It generates the same error. When stepping though the code, the date is in the correct format. When it errors out though, it references the month as being "00"
myCommand.Connection.Open()
            myQuery2 = "UPDATE tblMembership SET PasswordExpires = '" & Convert.ToDateTime(TextBox12.Text).ToString("yyyy-MM-dd") & ")' WHERE ID = '" & Label1.Text & "'"
            myCommand.CommandText = myQuery2
            myCommand.ExecuteNonQuery()
            myConnection.Close()

Open in new window