質問

I got an OleDbException saying that "Missing semicolon (;) at end of SQL statement." I got confused if the UPDATE statement is correct or not. In my code, I test the update statement using a button. Can anyone help me? I want to add 1 to the variable in order to increase its value; number of bottles. I am using Microsoft Access Database. Primary id is ID then Unique is room_number.

This is my Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '====test number of bottles=============
    Dim bottlecount As Integer 'variable used in order to increase the value of no. of bottle/s used

    bottlecount = Form3.lblBottle.Text
    bottlecount += 1
    Form3.lblBottle.Text = bottlecount

    roomhold = 1

    Dim statement As String = "UPDATE tblPatientInfo SET bottle_used='" & bottlecount & "' WHERE room_number= '" & roomhold & "' ORDER BY ID ;"
    Dim cmd As New OleDbCommand

    With cmd
        .CommandText = statement
        .Connection = Conn
        Conn.Open()
        .ExecuteNonQuery()
    End With
    Conn.Close()

End Sub

===after applying the changes========== This is my code:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim bottlecount As Integer = CInt(Form3.lblBottle.Text) + 1
    Form3.lblBottle.Text = bottlecount

    roomhold = 1

    Dim statement As String = "UPDATE tblPatientInfo SET bottle_used = @bottlecount"
    statement &= "WHERE room_number = @roomhold"

     Dim cmd As New OleDbCommand

    With cmd

        .Connection = Conn
        .CommandType = CommandType.Text
        .CommandText = statement
        .Parameters.AddWithValue("@bottlecount", bottlecount)
        .Parameters.AddWithValue("@roomhold", roomhold)
        Conn.Open()
        .ExecuteNonQuery()
    End With

Conn.Close()

Any help would be appreciated. Thanks!

役に立ちましたか?

解決

UPDATE is not allowed to have ORDER BY clause. If you want you update to be order, create a subquery of it which contains ordered list for updates. When using ADONet, please make sure your query is parameters. Get used of SQLCommand and Parameters. Try this edited code if it will work for you

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim bottlecount As Integer = cint(Form3.lblBottle.Text) + 1
    Form3.lblBottle.Text = bottlecount

    Dim roomhold AS Integer = 1

    Dim statement As String = "UPDATE tblPatientInfo SET bottle_used = @bottlecount " 
    statement &= "WHERE room_number = @roomhold  "
    Using conn as New SqlConnection("ConnectionStringHere")
        Using comm as New SqlCommand()
            With comm
                .Connection = conn
                .CommandType = CommandType.Text
                .CommandText = statement
                .Parameters.AddWithValue("@bottlecount", bottlecount )
                .Parameters.AddWithValue("@roomhold" , roomhold)
            End With
            Try
                conn.Open()
                comm.ExecuteNonQuery()
            Catch (ex as SQlException)
                ' add message here '
            Finally
                conn.Close()
            End Try
        End Using
    End Using
End Sub

他のヒント

You don't order update commands.

Dim statement As String = "UPDATE tblPatientInfo SET bottle_used='" & bottlecount & "' WHERE room_number= '" & roomhold & "'"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top