Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

sqlite - System.InvalidOperationException: 'Operation is not valid due to the current state of the object.' on a update query in vb.net

Hello I am trying to do an update query on my database however I end up with this error message

System.InvalidOperationException: 'Operation is not valid due to the current state of the object.'

Here is the code for my search query which I then use in the update

Private Function SearchData(Fname As String, ID As Int32) As DataTable
    Dim dt As New DataTable
    Dim ds As New DataSet

    Dim ssql As String = "SELECT * FROM customers WHERE fname LIKE @Fname OR CustomerID = @ID"
    Using con As New SQLiteConnection(ConStr),
     cmd As New SQLiteCommand(ssql, con)
        con.Open()
        cmd.Parameters.Add("@Fname", DbType.String).Value = $"%{Fname}%"
        cmd.Parameters.Add("@ID", DbType.Int32).Value = ID
        dt.Load(cmd.ExecuteReader)
        Dim da As New SQLiteDataAdapter(cmd)
        da.Fill(ds, "customers")
        dt = ds.Tables(0)
        If dt.Rows.Count > 0 Then
            ToTextbox(dt)
        End If
    End Using
    Return dt
End Function

This is the ToTextBox function

 Public Sub ToTextbox(ByVal newdt)

    txtFName.Text = newdt.Rows(0)(1).ToString()
    txtLName.Text = newdt.Rows(0)(2).ToString()
    mtxtContactNumber.Text = newdt.rows(0)(3).ToString()
    txtAddress.Text = newdt.rows(0)(4).ToString()
    txtTown.Text = newdt.rows(0)(5).ToString()
    txtPostCode.Text = newdt.rows(0)(6).ToString()



End Sub

And the update function

 Public Function updateguest(FirstName As String, ID As Integer) As Integer
    Dim Result As Integer
    Dim usql As String = "UPDATE Customers SET fname = @fname WHERE CustomerID = @ID;"
    Using con As New SQLiteConnection(ConStr),
        cmd As New SQLiteCommand(usql, con)
        con.Open()
        cmd.Parameters.Add("@fname", DbType.String).Value = FirstName
        cmd.Parameters.Add("@ID", DbType.Int32).Value = ID
        con.Open()
        Result = cmd.ExecuteNonQuery
        con.Close()
    End Using
    Return Result
End Function

And the Update button

    Private Sub IbtnUpdate_Click(sender As Object, e As EventArgs) Handles ibtnUpdate.Click
    Try
        Dim Result = updateguest(txtFName.Text, CInt(txtSearchID.Text))
        If Result > 0 Then
            MsgBox("New RECORD HAS BEEN UPDATED!")
        Else
            MsgBox("NO RECORD HAS BEEN UPDATDD!")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe you update problems are solved in comments.

You still have too much stuff in your DataAccess code. Just return the DataTable and use it in the user interface code.

Private Function SearchData(Fname As String, ID As Int32) As DataTable
    Dim dt As New DataTable
    Dim ssql As String = "SELECT * FROM customers WHERE fname LIKE @Fname OR CustomerID = @ID"
    Using con As New SQLiteConnection(ConStr),
            cmd As New SQLiteCommand(ssql, con)
        con.Open()
        cmd.Parameters.Add("@Fname", DbType.String).Value = $"%{Fname}%"
        cmd.Parameters.Add("@ID", DbType.Int32).Value = ID
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt = SearchData(FirstNameTextBox.Text, CInt(IDTextBox.Text))
    If dt.Rows.Count > 0 Then
        ToTextbox(dt)
    End If
End Sub

Public Sub ToTextbox(ByVal newdt As DataTable)
    txtFName.Text = newdt.Rows(0)(1).ToString()
    txtLName.Text = newdt.Rows(0)(2).ToString()
    mtxtContactNumber.Text = newdt.Rows(0)(3).ToString()
    txtAddress.Text = newdt.Rows(0)(4).ToString()
    txtTown.Text = newdt.Rows(0)(5).ToString()
    txtPostCode.Text = newdt.Rows(0)(6).ToString()
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...