oshirowanen
oshirowanen

Reputation: 15925

Textbox date to database

I have the following code which worked fine on a .net 1.1 website:

objSQLCommand.Parameters.Add(New SqlParameter("@date", SqlDbType.DateTime, 8))
objSQLCommand.Parameters("@date").Value = txtDate.Text

This doesn't work on a .net 3.5 server.

I'm getting a message saying that it can't convert a string to datetime.

Upvotes: 0

Views: 1122

Answers (1)

John Woo
John Woo

Reputation: 263693

Try this one,

objSQLCommand.Parameters("@date").Value = Convert.ToDate(txtDate.Text)

follow-up question, what is the format of the date in your textbox? Maybe you can take advantage of TryParse or ParseExact also.

Dim provider As CultureInfo = CultureInfo.InvariantCulture
Dim dateString as String = txtDate.Text '08/10/2012
Dim format As String = "d" 
objSQLCommand.Parameters("@date").Value = Date.ParseExact(dateString, format, provider)

Upvotes: 1

Related Questions