Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been using various code to convert a string in a textbox to datetime for an SQL database. This is what I recently used:

<br />
    Dim dtDate As DateTime<br />
<br />
dtDate = DateTime.ParseExact(txtDateFix.Text, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)<br />
       dtDate = DateTime.ParseExact(txtDateSite.Text, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)


I previously used this and it was unsuccessful:


txtDateFix.Text = dtDate.ToString("yyyyMMdd")<br />
   txtDateSite.Text = dtDate.ToString("yyyyMMdd")


I am using visual basic for an ASP.net project.

There is something I am unclear on-- from what I gather, using a ParseExact will convert the string to the format that is part of the code? If someone enters "03/25/2014" in the textbox, how do I modify this code so it inserts into the database as "20140325"? I keep running into an error that says the date is not compatible with the Gregorian calendar, which tells me my conversion isn't being handled. I'm not sure how to proceed.


Thanks!
Posted

Don't worry about formats too much - use DateTime.TryParse to convert the user set culture version, which is what the user is likely to use when he enters it! Or better, use a Calendar control or similar so that the user doesn't type it, and which returns you a DateTime value directly.

Then pass it to SQL as a Parameterised query, using the DateTime value directly - don't convert anything to strings to send to SQL, it understands datetimes and numbers directly passes as parameters.
VB
Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (insertDate) VALUES (@DT)", con)
        com.Parameters.AddWithValue("@DT", myDateTimeValue)
        com.ExecuteNonQuery()
    End Using
End Using
Using strings implies string concatenation to build an SQL command, and that is very, very dangerous - it allows SQL Injection which can damage or destroy your database.
 
Share this answer
 
Comments
21spirals 14-May-14 21:30pm    
What if my database has an entry where it expects a date in a certain column? Won't using @DT interfere with the stored procedures?
OriginalGriff 15-May-14 5:16am    
Sorry? That doesn't make a lot of sense to me - could you try explaining in better detail?
you need to convert the input date time text to DateTime type ( using DateTime.ParseExact); say it as dt

then you can use sql parameter to insert date time value like below. no need to convert it again to a string.
VB
cmd = New SqlCommand("INSERT INTO TableName(datetimeColumn) VALUES(@inputDateTime)", conn)
cmd.Parameters.AddWithValue("@inputDateTime", dt)' dt is DateTime value you converted from input text
cmd.ExecuteNonQuery()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900