我正在做一个待办事项列表程序,错误是当我点击保存时,它会显示错误,这是完整的代码,下面会有弹出错误消息的代码
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 = "]"