I have a piece of code that takes two strings, puts them together, then attempts to save that result into a table in a database. For some reason, the database is only showing the second half of the string ( Me.txtRev.text in this case). When in debug, I can see that newRev is actually correct, with the hyphenated string.

Any ideas anyone? Thanks in advance!

Dim newRev As String = Me.revs(0) & "-" & Me.txtRev.Text

Try
   Dim sqlSOPDept As New SqlClient.SqlCommand
   Dim sqlConn As New SqlClient.SqlConnection("Data Source=IMS-SQL-01;Initial Catalog=ImageTraining;Integrated Security=True")
   sqlSOPDept.CommandText = "UPDATE dbo.docHeader SET revision =" & newRev & " WHERE model = '" & Me.cmbModel.SelectedValue.ToString & "' AND part = '" & Me.cmbPart.SelectedValue.ToString & "'"
   sqlSOPDept.CommandType = CommandType.Text
   sqlSOPDept.Connection = sqlConn
   sqlConn.Open()

   sqlSOPDept.ExecuteNonQuery()

   sqlConn.Close()
Catch ex As Exception
   MsgBox(ex.ToString)
End Try

Recommended Answers

All 3 Replies

Your error is in the Update query. You mixed up one and double quotes. Do it this way:

sqlSOPDept.CommandText = "UPDATE dbo.docHeader SET revision = '" & newRev & "' WHERE model = '" & Convert.ToString(Me.cmbModel.SelectedValue.ToString) & "' AND part = '" & Convert.ToString(Me.cmbPart.SelectedValue.ToString) & "'"

Sometimes it just takes another pair of eyes. Thank you very much. Works like a charm!

You are welcome.
bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.