OleDBException未处理发生了什么?

时间:2014-04-23 15:11:29

标签: vb.net unhandled oledbexception

我正在做一个待办事项列表程序,错误是当我点击保存时,它会显示错误 这里是完整的代码,下面将会弹出错误信息的代码

Public Class frmForm1

Dim inc As Integer = 0
Dim MaxRows As Integer

Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Private Sub frmNavigate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\project.accdb;"
    con.Open()
    sql = "SELECT * from todolist"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "todolist")

    con.Close()

    MaxRows = ds.Tables("todolist").Rows.Count

    txtitem.Text = ds.Tables("todolist").Rows(inc).Item(1)
    txtdescript.Text = ds.Tables("todolist").Rows(inc).Item(2)
    dtptoday.Text = ds.Tables("todolist").Rows(inc).Item(3)
    dtpcomplete.Text = ds.Tables("todolist").Rows(inc).Item(4)

End Sub
Private Sub NavigateRecords()

    txtitem.Text = ds.Tables("todolist").Rows(inc).Item(1)
    txtdescript.Text = ds.Tables("todolist").Rows(inc).Item(2)
    dtptoday.Text = ds.Tables("todolist").Rows(inc).Item(3)
    dtpcomplete.Text = ds.Tables("todolist").Rows(inc).Item(4)

End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    If inc <> MaxRows - 1 Then
        inc = inc + 1
        NavigateRecords()
    Else
        MsgBox("No More Rows")
    End If

End Sub

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
    If inc > 0 Then
        inc = inc - 1
        NavigateRecords()
    ElseIf inc = -1 Then
        MsgBox("No Records Yet")
    ElseIf inc = 0 Then
        MsgBox("First Record")
    End If
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    If inc <> MaxRows - 1 Then
        inc = MaxRows - 1
        NavigateRecords()
    End If

End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    If inc <> 0 Then
        inc = 0
        NavigateRecords()
    End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    ds.Tables("todolist").Rows(inc).Item(1) = txtitem.Text
    ds.Tables("todolist").Rows(inc).Item(2) = txtdescript.Text
    ds.Tables("todolist").Rows(inc).Item(3) = dtptoday.Text
    ds.Tables("todolist").Rows(inc).Item(4) = dtpcomplete.Text


    da.Update(ds, "todolist")

    MsgBox("Data updated!")

End Sub

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
    btnSave.Enabled = True
    btnadd.Enabled = False
    btnUpdate.Enabled = False
    btnremove.Enabled = False


    txtitem.Clear()
    txtdescript.Clear()


End Sub

Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
    btnSave.Enabled = False
    btnadd.Enabled = True
    btnUpdate.Enabled = True
    btnremove.Enabled = True

    inc = 0
    NavigateRecords()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("todolist").NewRow()

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        da.Update(ds, "todolist")
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub

Private Sub btnremove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnremove.Click
    If MessageBox.Show("Do you really want to Delete this Record?", _
        "Delete", MessageBoxButtons.YesNo, _
        MessageBoxIcon.Warning) = DialogResult.No Then

        MsgBox("Operation Cancelled")
        Exit Sub
    End If

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    ds.Tables("todolist").Rows(inc).Delete()
    MaxRows = MaxRows - 1

    inc = 0
    NavigateRecords()
    da.Update(ds, "todolist")

End Sub


Private Sub btnsummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsummary.Click

    Me.Hide()
    summary.Show()
End Sub
End Class

错误发生在

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("todolist").NewRow()

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        da.Update(ds, "todolist") 'ERROR!!!!!
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub

这一行

da.Update(ds, "todolist")

我已经通过添加此

解决了这个问题
Dim cb As New OleDb.OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"

1 个答案:

答案 0 :(得分:1)

在您的Sub frmNavigate_Load()中,您关闭了连接。要解决此问题,您可以1)保持连接打开,或者2)每次打算执行数据操作时再次打开它(推荐)。

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("todolist").NewRow()

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        'reopen a connection for your da
        con.open()
        'now you are connected, you can build an INSERT command and then save
        cb.GetInsertCommand()
        da.Update(ds, "todolist")
        'close it again
        con.close()
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub