URL and Link repository

Tuesday, December 5, 2017

Inserting Dates and Times from Visual Basic Web Form to MySQL Database

Not pretty but it resolves that invalid string error.

ASP code / mark up:
<asp:TextBox> TextMode='Date' ID='StartDateBox' runat='server'</asp:TextBox>
<asp:TextBox> TextMode='Time' ID='StartTimeBox'runat='server'</asp:TextBox>
Visual Basic server code:
Dim sTime As DateTime = Convert.ToDateTime(StartTimeBox.Text.toString) 
Dim insertstring as String='INSERT INTO [sometable] (Start_Date, Start_Time) values (@sd, @st)'
Dim mySqlcmnd = New MySqlCommand(insertstring, [your db connection])
mySqlcmnd.Parameters.AddWithValue('sd', StartDateBox.Text)
' 24 hour format
mySqlcmnd.Parameters.AddWithValue('st', sTime.ToString("HH:mm"))
Dim rowCnt as Integer = mySqlcmnd.ExecuteNonQuery