Evening All,

i am trying to input 72003 3131/1 from text from a text field into sql
as a string.

I have the following code but getting an error
Error while inserting record on table, incorrect syntax near '3131'.


the end user will be able to change this text to anything they like and still
the exe will run.

Private Sub line2add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles line2add.Click
        Dim NewLongDesc As String
        Dim RConv As String
        Dim StkCodeV As String

        RConv = Me.txtLine2Rcon.Text
        NewLongDesc = Me.txtLongDesc.Text
        StkCodeV = Me.txtStockCode.Text

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Try
            con.ConnectionString = "Data Source=10.10.0.25;Initial Catalog=RConditions;Persist Security Info=True;User ID=mbish;Password=mbish"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO LN2(Stkcode, LongDesc,RCon) VALUES(" & StkCodeV & "," & NewLongDesc & "," & RConv & ")"


            cmd.ExecuteNonQuery()

        Catch ex As Exception
            MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
        Finally
            con.Close()
        End Try



    End Sub

please help

Recommended Answers

All 2 Replies

I want to force a user to input text into a text box and if there is nothing in the text box to show a msgbox but not to carry on with the code.

If Me.txtLine2Rcon.Text = "" Then MsgBox("Please insert a running condition for Line 2 before you add it to the database", vbOKOnly)

        Dim NewLongDesc As String
        Dim RConv As String
        Dim StkCodeV As String

        RConv = Me.txtLine2Rcon.Text
        NewLongDesc = Me.txtLongDesc.Text
        StkCodeV = Me.txtStockCode.Text

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Try
            con.ConnectionString = "Data Source=10.10.0.25;Initial Catalog=RConditions;Persist Security Info=True;User ID=mbish;Password=mbish"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO LN2(Stkcode, LongDesc,RCon) VALUES(@p1,@p2,@p3)"
            cmd.Parameters.AddWithValue("@p1", StkCodeV)
            cmd.Parameters.AddWithValue("@p2", NewLongDesc)
            cmd.Parameters.AddWithValue("@p3", RConv)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
        Finally
            con.Close()
        End Try



    End Sub
If Me.txtLine2Rcon.Text = "" Then MsgBox("Please insert a running condition for Line  before you add it to the database", vbOKOnly)
exit sub
Else
 
        Dim NewLongDesc As String
        Dim RConv As String
        Dim StkCodeV As String
 
        RConv = Me.txtLine2Rcon.Text
        NewLongDesc = Me.txtLongDesc.Text
        StkCodeV = Me.txtStockCode.Text
 
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Try
            con.ConnectionString = "Data Source=10.10.0.25;Initial Catalog=RConditions;Persist Security Info=True;User ID=mbish;Password=mbish"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO LN2(Stkcode, LongDesc,RCon) VALUES(@p1,@p2,@p3)"
            cmd.Parameters.AddWithValue("@p1", StkCodeV)
            cmd.Parameters.AddWithValue("@p2", NewLongDesc)
            cmd.Parameters.AddWithValue("@p3", RConv)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
        Finally
            con.Close()
        End Try
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.