我的提交按钮的代码昨天的大部分时间都是按照下面的方式编写的。昨天我会定期得到这个例外,但今天我每次都会得到它。它失败了:
Dim newRow As DataRow = ds.Tables(" PV_Table")。NewRow()
它说newRow是什么,我怎么能解决这个问题以及它应该是什么?
Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click
Dim newRow As DataRow = ds.Tables("PV_Table").NewRow()
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
newRow("Location") = txtLocation.Text
newRow("Project Name") = txtProjectName.Text
newRow("Pipe Date") = txtPipeDate.Text
newRow("Pipe Number") = txtPipeNumber.text
newRow("Pipe Size") = txtPipeSize.Text
newRow("Aprox Footage") = txtAproxFootage.Text
newRow("Install Date") = txtInstallDate.Text
ds.Tables("PV_Table").Rows.Add(newRow)
da.Update(ds, "PV_Table")
Me.PV_TableTableAdapter.Fill(Me.PVdbDataSet.PV_Table) 'DataGridView Binding
'SET UP THE PROVIDER
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
'SET THE DATABASE AND WHERE THE DATABASE IS
TheDatabase = "/PVdb.accdb"
MyFolder = "C:\Pipe Vault"
FullDatabasePath = MyFolder & TheDatabase
'SET THE DATA SOURCE
dbSource = "Data Source = " & FullDatabasePath
'SET THE CONNECTION STRING
con.ConnectionString = dbProvider & dbSource
'OPEN THE DATABASE
con.Open()
'STORE THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
sql = "SELECT * FROM PV_table"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "PV_Table")
'Close THE DATABASE '
con.Close()
'ENABLES TEXT BOXES
For Each a In Me.Controls
If TypeOf a Is TextBox Then
a.Enabled = False
End If
Next
'CLEARS CONTENTS OF TEXT BOXES
txtLocation.Clear()
txtProjectName.Clear()
txtPipeDate.Clear()
txtPipeNumber.Clear()
txtPipeSize.Clear()
txtAproxFootage.Clear()
txtInstallDate.Clear()
'SETS STATES OF BUTTONS
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = False
btnDelete.Enabled = False
End Sub
我在Form Load事件中发现,我在评论'存储SQL STRING和CONNECTION OBJECT TO DATA_ADAPTER下有相同的代码。这一切都被评论出来了。我取消了它们,它似乎再次起作用。
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.PV_TableTableAdapter.Fill(Me.PVdbDataSet.PV_Table)
'SET UP THE PROVIDER
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
'SET THE DATABASE AND WHERE THE DATABASE IS
TheDatabase = "/PVdb.accdb"
MyFolder = "C:\Pipe Vault"
FullDatabasePath = MyFolder & TheDatabase
'SET THE DATA SOURCE
dbSource = "Data Source = " & FullDatabasePath
'SET THE CONNECTION STRING
con.ConnectionString = dbProvider & dbSource
'OPEN THE DATABASE
con.Open()
'STORE THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
sql = "SELECT * FROM PV_table"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "PV_Table")
'Close THE DATABASE '
con.Close()
'MessageBox.Show("Database is now closed")
DataGridView1.GridColor = Color.Red
End Sub