We help IT Professionals succeed at work.

Updating date/time from VB.NET to SQL

621 Views
Last Modified: 2012-05-11
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?
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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

Author

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

Commented:
You need pull data from SQL  then replace date with textbox.
using same time

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

Author

Commented:
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??
Try this:

dim cmdText as String

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

Commented:
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

Commented:
*
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

Author

Commented:
Look at the error...

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

Commented:
Could you post your code?

Author

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.