smm6809
asked on
Error updating SQL server table with vb.net
I get a syntax error near dtTo. Not sure if dates are handled differently or if it is something else. The select statement is hitting the Case False. Code below...
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = sqlComm
con.Open()
cmd.Connection = con
Select Case ysnNew
Case False
cmd.CommandText = "Update tblLeads " & vbCrLf &
"SET strType = @strType," & vbCrLf &
"numClient = @numClient," & vbCrLf &
"strLast = @strLast," & vbCrLf &
"strFirst = @strFirst" & vbCrLf &
"dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &
"tmTo = @tmTo, sqldbtypes.Time" & vbCrLf &
"dtCTC = @dtCT, SqlDbTypes.DateTime" & vbCrLf &
"tmCTC = @tmCT, sqldbtypes.Time " & vbCrLf &
"strNotes = @strNotes" & vbCrLf &
"strResponse = @strResponse" & vbCrLf &
"strLeadType = @strLeadType" & vbCrLf &
"ysnBad = @ysnBad" & vbCrLf &
"ysnPhone = @ysnPhone" & vbCrLf &
"Where numLeadID = @numLeadID"
Case Else
cmd.CommandText = "Insert Into tblLeads " & vbCrLf &
"strType = @strType," & vbCrLf &
"numClient = @numClient," & vbCrLf &
"strLast = @strLast," & vbCrLf &
"strFirst = @strFirst" & vbCrLf &
"dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &
"tmTo = @tmTo, sqldbtypes.Time" & vbCrLf &
"dtCTC = @dtCT, SqlDbTypes.DateTime" & vbCrLf &
"tmCTC = @tmCT, sqldbtypes.Time " & vbCrLf &
"strNotes = @strNotes" & vbCrLf &
"strResponse = @strResponse" & vbCrLf &
"strLeadType = @strLeadType" & vbCrLf &
"ysnBad = @ysnBad" & vbCrLf &
"ysnPhone = @ysnPhone" & vbCrLf & ""
End Select
cmd.Parameters.AddWithValu e("@strTyp e", Me.txtType.Text)
cmd.Parameters.AddWithValu e("@numCli ent", Me.txtClient.Text)
cmd.Parameters.AddWithValu e("@strLas t", Me.txtlast.Text)
cmd.Parameters.AddWithValu e("@strFir st", Me.txtFirst.Text)
cmd.Parameters.AddWithValu e("@dtTo", Me.dtTO.Text)
cmd.Parameters.AddWithValu e("@tmTO", Me.tmTO.Text)
cmd.Parameters.AddWithValu e("@dtCT", Me.dtCT.Text)
cmd.Parameters.AddWithValu e("@tmCT", Me.dtCT.Text)
cmd.Parameters.AddWithValu e("@strNot es", Me.txtNotes.Text)
cmd.Parameters.AddWithValu e("@strRes ponse", Me.txtEmail.Text)
cmd.Parameters.AddWithValu e("@strLea dType", Me.cboVehicle.Text)
cmd.Parameters.AddWithValu e("@ysnBad ", Me.chkBad.Text)
cmd.Parameters.AddWithValu e("@ysnPho ne", Me.ysnPhone.Text)
Select Case ysnNew
Case False
cmd.Parameters.AddWithValu e("@numLea dID", Me.txtID.Text)
End Select
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = sqlComm
con.Open()
cmd.Connection = con
Select Case ysnNew
Case False
cmd.CommandText = "Update tblLeads " & vbCrLf &
"SET strType = @strType," & vbCrLf &
"numClient = @numClient," & vbCrLf &
"strLast = @strLast," & vbCrLf &
"strFirst = @strFirst" & vbCrLf &
"dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &
"tmTo = @tmTo, sqldbtypes.Time" & vbCrLf &
"dtCTC = @dtCT, SqlDbTypes.DateTime" & vbCrLf &
"tmCTC = @tmCT, sqldbtypes.Time " & vbCrLf &
"strNotes = @strNotes" & vbCrLf &
"strResponse = @strResponse" & vbCrLf &
"strLeadType = @strLeadType" & vbCrLf &
"ysnBad = @ysnBad" & vbCrLf &
"ysnPhone = @ysnPhone" & vbCrLf &
"Where numLeadID = @numLeadID"
Case Else
cmd.CommandText = "Insert Into tblLeads " & vbCrLf &
"strType = @strType," & vbCrLf &
"numClient = @numClient," & vbCrLf &
"strLast = @strLast," & vbCrLf &
"strFirst = @strFirst" & vbCrLf &
"dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &
"tmTo = @tmTo, sqldbtypes.Time" & vbCrLf &
"dtCTC = @dtCT, SqlDbTypes.DateTime" & vbCrLf &
"tmCTC = @tmCT, sqldbtypes.Time " & vbCrLf &
"strNotes = @strNotes" & vbCrLf &
"strResponse = @strResponse" & vbCrLf &
"strLeadType = @strLeadType" & vbCrLf &
"ysnBad = @ysnBad" & vbCrLf &
"ysnPhone = @ysnPhone" & vbCrLf & ""
End Select
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
Select Case ysnNew
Case False
cmd.Parameters.AddWithValu
End Select
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
also, it is the fist time I see this syntax:
I would be very surprised if you can specify a datatype like this! and I don't see any trace of if in the sql-bible: https://msdn.microsoft.com/en-us/library/ms177523.aspx
"dtTo = @dtTo, SqlDbTypes.DateTime" & vbCrLf &
I would be very surprised if you can specify a datatype like this! and I don't see any trace of if in the sql-bible: https://msdn.microsoft.com/en-us/library/ms177523.aspx
ASKER
Took SqlDbTypes.DateTime out of all places I referenced it. Fixed the comma issues and now getting: Error while inserting record... Conversion failed when converting date and/or time from character string.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
My time fields tmTO and tmCT don't like that. It says value is not a member of a textbox. I use a textbox for the user to enter the times (ie. 3:15 PM) because I have to measure how long it took for a task to be completed. Should I be doing it differently?
and what is the datatype in the database?
You will need to convert your text to a datetime value probably
You will need to convert your text to a datetime value probably
ASKER
In the database those fields are time(7)
I never tried those. There is no real .Net equivalent. You could at least try to convert from text to datetime:
Dim dtmTime As DateTime = DateTime.Parse(tmTO.Text)
cmd.Parameters.AddWithValue("@tmTO", dtmTime)
ASKER
That worked. Just have one question. Is there a way to format date and time to read month/day/year instead of the other way around?
read/format from where?
ASKER
In the sql table the date is formatted 2016-05-09. Does that have to be the format or can it be 05-09-2016?
it is usually not the job of SQL server to format dates.
but if you really need, you can use Format:
but if you really need, you can use Format:
SELECT FORMAT(GETDATE(), 'MM-dd-yyyy')
Open in new window