Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2007, 01:40 PM
Registered User
 
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to estrela666
Default date and time

Hi!
I have a form with 2 textboxes. One for date and the other for time.
How I save the time in my sql database in a field type datetime?
Here is my code:

Dim xDate As Date
Dim xTime As DateTime
xTime = txt_time.Text
xDate = txt_date.Text
Dim cmd_log As New SqlClient.SqlCommand("INSERT INTO MyTable (fld_date,fld_time) VALUES (@date,@time);", conn_log)
cmd_log.Parameters.AddWithValue("@date", Format(xDate, "yyyyMMdd"))
cmd_log.Parameters.AddWithValue("@time", xTime)
        conn_log.Open()

Do I have to format my variable xTime like I did for variable xDate?
Thanks :).
 
Old January 4th, 2007, 03:16 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I don't want to just sound critical right up front, but it's usually much easier to deal with storage of a date and time in a database with a single datetime field. Then you can just insert and deal with a single value.

With a single field you could use something like this to create a strongly typed datetime value from the two input strings:

DateTime.Parse("{0} {1}", date, time)

Using the date and time separately in two values requires more manual formatting.

-Peter
 
Old January 5th, 2007, 11:28 AM
Registered User
 
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to estrela666
Default

Hi planoie! Thanks for the help!
I modified my code with your sugestion, and it works fine!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Time from Date/Time Navy1991_1 XSLT 2 June 3rd, 2008 12:03 PM
date and time in EST time zone anboss XSLT 1 May 21st, 2008 01:42 PM
How to retieve only the date not date and time debjanib ASP.NET 1.0 and 1.1 Professional 2 July 25th, 2006 01:10 PM
insert system date. no date and time jimmy Access VBA 6 November 20th, 2003 01:11 PM
Date and Time scottma135 VB Components 0 August 26th, 2003 10:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.