当我填写所有字段并点击“保存”按钮获取记录时,我似乎无法弄清楚我在尝试保存新记录集时遇到的错误,我的表单加载了插入我的Access数据库我收到以下错误
(错误:System.Data.dll中发生类型' System.InvalidOperationException'未处理的异常 其他信息:更新无法找到TableMapping ['客户']或DataTable'客户'。)
代码
Imports System.Data.OleDb
Friend Class Client
Dim daClient As New OleDbDataAdapter()
Dim dsClient As New DataSet()
Public ID As String
Public State As AccessModule.FormState
Private Sub ClientBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.ClientBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.LoginDataSet)
End Sub
Private Sub Client_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'LoginDataSet.Client' table. You can move, or remove it, as needed.
Me.ClientTableAdapter.Fill(Me.LoginDataSet.Client)
Dim cnClient As OleDbConnection
cnClient = New OleDbConnection
Try
With cnClient
If .State = ConnectionState.Open Then .Close()
.ConnectionString = cnString
.Open()
End With
Catch ex As OleDbException
MsgBox(ex.ToString)
End Try
If State = AccessModule.FormState.adStateAddMode Then
Try
Dim qryClient As String = "SELECT * FROM Client"
daClient.SelectCommand = New OleDbCommand(qryClient, cnClient)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(daClient)
daClient.Fill(dsClient, "Client")
Catch ex As OleDbException
MsgBox(ex.ToString)
Finally
cnClient.Close()
End Try
ElseIf State = AccessModule.FormState.adStateEditMode Then
Dim qryClient As String = "SELECT * FROM Client WHERE ID = '" & ID & "'"
daClient.SelectCommand = New OleDbCommand(qryClient, cnClient)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(daClient)
daClient.Fill(dsClient, "Client")
Dim dt As DataTable = dsClient.Tables("Client")
Try
IDTextBox.Text = dt.Rows(0)("ID")
FnameTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Fname")), "", dt.Rows(0)("Fname"))
LnameTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Lname")), "", dt.Rows(0)("Lname"))
MnameTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Mname")), "", dt.Rows(0)("Mname"))
SexeComboBox.Text = IIf(IsDBNull(dt.Rows(0)("Sexe")), "", dt.Rows(0)("Sexe"))
BdateDateTimePicker.Text = IIf(IsDBNull(dt.Rows(0)("Bdate")), "", dt.Rows(0)("Bdate"))
AgeTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Age")), "", dt.Rows(0)("Age"))
DdateDateTimePicker.Text = IIf(IsDBNull(dt.Rows(0)("Ddate")), "", dt.Rows(0)("Ddate"))
DplaceTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Dplace")), "", dt.Rows(0)("Dplace"))
SINTextBox.Text = IIf(IsDBNull(dt.Rows(0)("SIN")), "", dt.Rows(0)("SIN"))
OccuTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Occu")), "", dt.Rows(0)("Occu"))
OccuTTextBox.Text = IIf(IsDBNull(dt.Rows(0)("OccuT")), "", dt.Rows(0)("OccuT"))
StreetTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Street")), "", dt.Rows(0)("Street"))
CityTextBox.Text = IIf(IsDBNull(dt.Rows(0)("CIty")), "", dt.Rows(0)("City"))
ProvTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Prov")), "", dt.Rows(0)("Prov"))
CountryTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Country")), "", dt.Rows(0)("Country"))
PcodeTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Pcode")), "", dt.Rows(0)("Pcode"))
TelTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Tel")), "", dt.Rows(0)("Tel"))
FaxTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Fax")), "", dt.Rows(0)("Fax"))
EmailTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Email")), "", dt.Rows(0)("Email"))
ID1TextBox.Text = IIf(IsDBNull(dt.Rows(0)("ID1")), "", dt.Rows(0)("ID1"))
ID2TextBox.Text = IIf(IsDBNull(dt.Rows(0)("ID2")), "", dt.Rows(0)("ID2"))
ID3TextBox.Text = IIf(IsDBNull(dt.Rows(0)("ID3")), "", dt.Rows(0)("ID3"))
LicenseTextBox.Text = IIf(IsDBNull(dt.Rows(0)("License")), "", dt.Rows(0)("License"))
RelationComboBox.Text = IIf(IsDBNull(dt.Rows(0)("Relation")), "", dt.Rows(0)("Relation"))
RoleComboBox.Text = IIf(IsDBNull(dt.Rows(0)("Role")), "", dt.Rows(0)("Role"))
Vday1DateTimePicker.Text = IIf(IsDBNull(dt.Rows(0)("Vday1")), "", dt.Rows(0)("Vday1"))
Vday1LocTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Vday1Loc")), "", dt.Rows(0)("Vday1Loc"))
Vday1YTimeTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Vday1YTime")), "", dt.Rows(0)("Vday1YTime"))
Vday2DateTimePicker.Text = IIf(IsDBNull(dt.Rows(0)("Vday2")), "", dt.Rows(0)("Vday2"))
Vday2LocTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Vday2Loc")), "", dt.Rows(0)("Vday2Loc"))
Vday2TimeTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Vday2Time")), "", dt.Rows(0)("Vday2Time"))
Prayer1DateTimePicker.Text = IIf(IsDBNull(dt.Rows(0)("Prayer1")), "", dt.Rows(0)("Prayer1"))
Prayer1TimeTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Prayer1Time")), "", dt.Rows(0)("Prayer1Time"))
Prayer1LocTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Prayer1Loc")), "", dt.Rows(0)("Prayer1Loc"))
Service1DateTimePicker.Text = IIf(IsDBNull(dt.Rows(0)("Service1")), "", dt.Rows(0)("Service1"))
Service1LocTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Service1Loc")), "", dt.Rows(0)("Service1Loc"))
Service1TimeTextBox.Text = IIf(IsDBNull(dt.Rows(0)("Service1Time")), "", dt.Rows(0)("Service1Time"))
DispoTypeComboBox.Text = IIf(IsDBNull(dt.Rows(0)("DispoType")), "", dt.Rows(0)("DispoType"))
DispoDateDateTimePicker.Text = IIf(IsDBNull(dt.Rows(0)("DispoDate")), "", dt.Rows(0)("DispoDate"))
DispoTimeTextBox.Text = IIf(IsDBNull(dt.Rows(0)("DispoTime")), "", dt.Rows(0)("DispoTime"))
DispoLocTextBox.Text = IIf(IsDBNull(dt.Rows(0)("DispoLoc")), "", dt.Rows(0)("DispoLoc"))
Catch ex As OleDbException
MsgBox(ex.ToString)
Finally
cnClient.Close()
End Try
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim dt As DataTable = dsClient.Tables("Client")
If IDTextBox.Text = "" Then
MsgBox("Please fill At Need Number", MsgBoxStyle.Critical)
Exit Sub
End If
Try
If State = AccessModule.FormState.adStateAddMode Then
' add a row
Dim newRow As DataRow
newRow = dt.NewRow()
newRow("ID") = IDTextBox.Text
newRow("Fname") = IIf(FnameTextBox.Text = "", System.DBNull.Value, FnameTextBox.Text)
newRow("Lname") = IIf(LnameTextBox.Text = "", System.DBNull.Value, LnameTextBox.Text)
newRow("Mname") = IIf(MnameTextBox.Text = "", System.DBNull.Value, MnameTextBox.Text)
newRow("Sexe") = IIf(SexeComboBox.Text = "", System.DBNull.Value, SexeComboBox.Text)
newRow("Bdate") = IIf(BdateDateTimePicker.Text = "", System.DBNull.Value, BdateDateTimePicker.Text)
newRow("Bplace") = IIf(BplaceTextBox.Text = "", System.DBNull.Value, BplaceTextBox.Text)
newRow("Age") = IIf(AgeTextBox.Text = "", System.DBNull.Value, AgeTextBox.Text)
newRow("Ddate") = IIf(DdateDateTimePicker.Text = "", System.DBNull.Value, DdateDateTimePicker.Text)
newRow("Dplace") = IIf(DplaceTextBox.Text = "", System.DBNull.Value, DplaceTextBox.Text)
newRow("SIN") = IIf(SINTextBox.Text = "", System.DBNull.Value, SINTextBox.Text)
newRow("Occu") = IIf(OccuTextBox.Text = "", System.DBNull.Value, OccuTextBox.Text)
newRow("OccuT") = IIf(OccuTTextBox.Text = "", System.DBNull.Value, OccuTTextBox.Text)
newRow("Street") = IIf(StreetTextBox.Text = "", System.DBNull.Value, StreetTextBox.Text)
newRow("City") = IIf(CityTextBox.Text = "", System.DBNull.Value, CityTextBox.Text)
newRow("Country") = IIf(CountryTextBox.Text = "", System.DBNull.Value, CountryTextBox.Text)
newRow("Pcode") = IIf(PcodeTextBox.Text = "", System.DBNull.Value, PcodeTextBox.Text)
newRow("Tel") = IIf(TelTextBox.Text = "", System.DBNull.Value, TelTextBox.Text)
newRow("Fax") = IIf(FaxTextBox.Text = "", System.DBNull.Value, FaxTextBox.Text)
newRow("Email") = IIf(EmailTextBox.Text = "", System.DBNull.Value, EmailTextBox.Text)
newRow("ID1") = IIf(ID1TextBox.Text = "", System.DBNull.Value, ID1TextBox.Text)
newRow("ID2") = IIf(ID2TextBox.Text = "", System.DBNull.Value, ID2TextBox.Text)
newRow("ID3") = IIf(ID3TextBox.Text = "", System.DBNull.Value, ID3TextBox.Text)
newRow("License") = IIf(LicenseTextBox.Text = "", System.DBNull.Value, LicenseTextBox.Text)
newRow("Relation") = IIf(RelationComboBox.Text = "", System.DBNull.Value, RelationComboBox.Text)
newRow("Role") = IIf(RoleComboBox.Text = "", System.DBNull.Value, RoleComboBox.Text)
newRow("Vday1") = IIf(Vday1DateTimePicker.Text = "", System.DBNull.Value, Vday1DateTimePicker.Text)
newRow("Vday1Loc") = IIf(Vday1LocTextBox.Text = "", System.DBNull.Value, Vday1LocTextBox.Text)
newRow("Vday1YTime") = IIf(Vday1YTimeTextBox.Text = "", System.DBNull.Value, Vday1YTimeTextBox.Text)
newRow("Vday2") = IIf(Vday2DateTimePicker.Text = "", System.DBNull.Value, Vday2DateTimePicker.Text)
newRow("Vday2Loc") = IIf(Vday2LocTextBox.Text = "", System.DBNull.Value, Vday2LocTextBox.Text)
newRow("Vday2Time") = IIf(Vday2TimeTextBox.Text = "", System.DBNull.Value, Vday2TimeTextBox.Text)
newRow("Prayer1") = IIf(Prayer1DateTimePicker.Text = "", System.DBNull.Value, Prayer1DateTimePicker.Text)
newRow("Prayer1Time") = IIf(Prayer1TimeTextBox.Text = "", System.DBNull.Value, Prayer1TimeTextBox.Text)
newRow("Prayer1Loc") = IIf(Prayer1LocTextBox.Text = "", System.DBNull.Value, Prayer1LocTextBox.Text)
newRow("Service1") = IIf(Service1DateTimePicker.Text = "", System.DBNull.Value, Service1DateTimePicker.Text)
newRow("Service1Loc") = IIf(Service1LocTextBox.Text = "", System.DBNull.Value, Service1LocTextBox.Text)
newRow("Service1Time") = IIf(Service1TimeTextBox.Text = "", System.DBNull.Value, Service1TimeTextBox.Text)
newRow("DispoType") = IIf(DispoTypeComboBox.Text = "", System.DBNull.Value, DispoTypeComboBox.Text)
newRow("DispoDate") = IIf(DispoDateDateTimePicker.Text = "", System.DBNull.Value, DispoDateDateTimePicker.Text)
newRow("DispoTime") = IIf(DispoTimeTextBox.Text = "", System.DBNull.Value, DispoTimeTextBox.Text)
newRow("DispoLoc") = IIf(DispoLocTextBox.Text = "", System.DBNull.Value, DispoLocTextBox.Text)
dt.Rows.Add(newRow)
Else
With dt
.Rows(0)("ID") = IDTextBox.Text
.Rows(0)("Fname") = IIf(FnameTextBox.Text = "", System.DBNull.Value, FnameTextBox.Text)
.Rows(0)("Lname") = IIf(LnameTextBox.Text = "", System.DBNull.Value, LnameTextBox.Text)
.Rows(0)("Mname") = IIf(MnameTextBox.Text = "", System.DBNull.Value, MnameTextBox.Text)
.Rows(0)("Sexe") = IIf(SexeComboBox.Text = "", System.DBNull.Value, SexeComboBox.Text)
.Rows(0)("Bdate") = IIf(BdateDateTimePicker.Text = "", System.DBNull.Value, BdateDateTimePicker.Text)
.Rows(0)("Bplace") = IIf(BplaceTextBox.Text = "", System.DBNull.Value, BplaceTextBox.Text)
.Rows(0)("Age") = IIf(AgeTextBox.Text = "", System.DBNull.Value, AgeTextBox.Text)
.Rows(0)("Ddate") = IIf(DdateDateTimePicker.Text = "", System.DBNull.Value, DdateDateTimePicker.Text)
.Rows(0)("Dplace") = IIf(DplaceTextBox.Text = "", System.DBNull.Value, DplaceTextBox.Text)
.Rows(0)("SIN") = IIf(SINTextBox.Text = "", System.DBNull.Value, SINTextBox.Text)
.Rows(0)("Occu") = IIf(OccuTextBox.Text = "", System.DBNull.Value, OccuTextBox.Text)
.Rows(0)("OccuT") = IIf(OccuTTextBox.Text = "", System.DBNull.Value, OccuTTextBox.Text)
.Rows(0)("Street") = IIf(StreetTextBox.Text = "", System.DBNull.Value, StreetTextBox.Text)
.Rows(0)("City") = IIf(CityTextBox.Text = "", System.DBNull.Value, CityTextBox.Text)
.Rows(0)("Country") = IIf(CountryTextBox.Text = "", System.DBNull.Value, CountryTextBox.Text)
.Rows(0)("Pcode") = IIf(PcodeTextBox.Text = "", System.DBNull.Value, PcodeTextBox.Text)
.Rows(0)("Tel") = IIf(TelTextBox.Text = "", System.DBNull.Value, TelTextBox.Text)
.Rows(0)("Fax") = IIf(FaxTextBox.Text = "", System.DBNull.Value, FaxTextBox.Text)
.Rows(0)("Email") = IIf(EmailTextBox.Text = "", System.DBNull.Value, EmailTextBox.Text)
.Rows(0)("ID1") = IIf(ID1TextBox.Text = "", System.DBNull.Value, ID1TextBox.Text)
.Rows(0)("ID2") = IIf(ID2TextBox.Text = "", System.DBNull.Value, ID2TextBox.Text)
.Rows(0)("ID3") = IIf(ID3TextBox.Text = "", System.DBNull.Value, ID3TextBox.Text)
.Rows(0)("License") = IIf(LicenseTextBox.Text = "", System.DBNull.Value, LicenseTextBox.Text)
.Rows(0)("Relation") = IIf(RelationComboBox.Text = "", System.DBNull.Value, RelationComboBox.Text)
.Rows(0)("Role") = IIf(RoleComboBox.Text = "", System.DBNull.Value, RoleComboBox.Text)
.Rows(0)("Vday1") = IIf(Vday1DateTimePicker.Text = "", System.DBNull.Value, Vday1DateTimePicker.Text)
.Rows(0)("Vday1Loc") = IIf(Vday1LocTextBox.Text = "", System.DBNull.Value, Vday1LocTextBox.Text)
.Rows(0)("Vday1YTime") = IIf(Vday1YTimeTextBox.Text = "", System.DBNull.Value, Vday1YTimeTextBox.Text)
.Rows(0)("Vday2") = IIf(Vday2DateTimePicker.Text = "", System.DBNull.Value, Vday2DateTimePicker.Text)
.Rows(0)("Vday2Loc") = IIf(Vday2LocTextBox.Text = "", System.DBNull.Value, Vday2LocTextBox.Text)
.Rows(0)("Vday2Time") = IIf(Vday2TimeTextBox.Text = "", System.DBNull.Value, Vday2TimeTextBox.Text)
.Rows(0)("Prayer1") = IIf(Prayer1DateTimePicker.Text = "", System.DBNull.Value, Prayer1DateTimePicker.Text)
.Rows(0)("Prayer1Time") = IIf(Prayer1TimeTextBox.Text = "", System.DBNull.Value, Prayer1TimeTextBox.Text)
.Rows(0)("Prayer1Loc") = IIf(Prayer1LocTextBox.Text = "", System.DBNull.Value, Prayer1LocTextBox.Text)
.Rows(0)("Service1") = IIf(Service1DateTimePicker.Text = "", System.DBNull.Value, Service1DateTimePicker.Text)
.Rows(0)("Service1Loc") = IIf(Service1LocTextBox.Text = "", System.DBNull.Value, Service1LocTextBox.Text)
.Rows(0)("Service1Time") = IIf(Service1TimeTextBox.Text = "", System.DBNull.Value, Service1TimeTextBox.Text)
.Rows(0)("DispoType") = IIf(DispoTypeComboBox.Text = "", System.DBNull.Value, DispoTypeComboBox.Text)
.Rows(0)("DispoDate") = IIf(DispoDateDateTimePicker.Text = "", System.DBNull.Value, DispoDateDateTimePicker.Text)
.Rows(0)("DispoTime") = IIf(DispoTimeTextBox.Text = "", System.DBNull.Value, DispoTimeTextBox.Text)
.Rows(0)("DispoLoc") = IIf(DispoLocTextBox.Text = "", System.DBNull.Value, DispoLocTextBox.Text)
End With
End If
daClient.Update(dsClient, "Clients")
MsgBox("Record successfully saved.", MsgBoxStyle.Information)
Catch ex As OleDbException
MsgBox(ex.ToString)
End Try
End Sub
End Class
任何想法?
答案 0 :(得分:1)
表格名称"Client"
无处不在,除了这一行:
daClient.Update(dsClient, "Clients")
我打赌你需要将其改为
daClient.Update(dsClient, "Client")