Getting a date from a textfield to sql server

osani

Member
Joined
Apr 7, 2005
Messages
14
Programming Experience
1-3
Hi
I am trying to get a date from a textfield in vb.net to sql server 2000. The data type is datetime. The problem is no matter what I enter or how I enter it, the entry goes in to the db as 1/1/1900. I've included pieces of relevant code. Please help. Thanks

If IsDate(textdate.Text) = FalseThen
MsgBox("please enter a valid date")
Else
Dim str AsString = "insert demographic(accnum, clinic, qdate) values (" & _
acc & "," & _
preparestr(textclinic.Text) & "," & _
textdate.Text & ")"
mycommand = New SqlCommand(str, myconnection)
mycommand.ExecuteNonQuery()
End If
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Date's need to be surrounded by #'s, just like strings need '.
VB.NET:
[/color]
[color=#0000ff]#" & _
textdate.Text & "#)"[/color]
[color=#0000ff]
 

osani

Member
Joined
Apr 7, 2005
Messages
14
Programming Experience
1-3
the # didn't work

changed code to:
Dim str AsString = "insert demographic(quest_date, accnum, clinic) values (" & _
"#" & textdate.Text & "#" & "," & _
acc & "," & _
preparestr(textclinic.Text) & ")"


When I put in today's date, I get the error:
"The name '#4' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.


 

osani

Member
Joined
Apr 7, 2005
Messages
14
Programming Experience
1-3
nope, that doesn't matter (i did try it though)

the database is getting the entry with the right account number and clinic name, but recieving a date of 1/1/1900. The wierdest part is, I have date of birth entered on the next form and it takes the date just fine.

Thanks.
 

osani

Member
Joined
Apr 7, 2005
Messages
14
Programming Experience
1-3
No, it is still stored as a datetime datatype and will only take date formatted strings but I have this code for the text boxes to trim and input the strings with ' ' and the date needed that too.

Public Function preparestr(ByVal strvalue As String) As String

If strvalue.Trim() = "" Then
Return "NULL"
Else
Return "'" & strvalue.Trim() & "'"
End If
End Function

 
Top Bottom