我正在使用visual studio 2010构建我的应用程序和SQL Server 2005来存储数据。我创建了一个表适配器,数据集并将控件绑定到我已经制作的表单。事实是,我正在尝试将新记录插入数据库,它将无法正常工作!我可以使用数据库中的记录填写表单并更新它,以便我知道它确实正确连接。 每当我尝试通过表适配器或手动插入新记录时,我似乎无法将其自动保存到数据库中。
我把它放在保存按钮下
Private Sub SaveBtn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理SaveBtn.Click
Dim CustomerRow As accountingdbDataSet.CustomerRow
CustomerRow = Me.AccountingdbDataSet._Customer.NewCustomerRow()
CustomerRow.customerID = CustomerIDBox.Text
CustomerRow.customerName = FirstNameBox.Text
CustomerRow.customerSurname = SurnameBox.Text
CustomerRow.customerAddress1 = AddressBox1.Text
CustomerRow.customerAddress2 = AddressBox2.Text
CustomerRow.customerAddress3 = AddressBox3.Text
CustomerRow.customerPostcode = PostcodeBox.Text
CustomerRow.customerTelArea = AreaBox.Text
CustomerRow.customerTelNumber = NumberBox.Text
CustomerRow.customerEmail = EmailAddressBox.Text
' Add the row to the Region table
Me.AccountingdbDataSet._Customer.Rows.Add(CustomerRow)
' Save the new row to the database
Me.CustomerTableAdapter.Update(Me.AccountingdbDataSet._Customer)
End Sub
但这不起作用。它抱怨NullException,即使我的数据库中的字段未设置为null(除了customerID,因为它是主键)。我也试过了
Private Sub SaveBtn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理SaveBtn.Click
Try
Me.Validate()
Me.CustomerBindingSource.EndEdit()
Me.CustomerTableAdapter.Update(Me.AccountingdbDataSet)
MessageBox.Show("Updates to the database have been successful.", "Successful Updates", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Updates to the database have failed.", "Unsuccessful Updates", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
只允许我更新当前记录 - 不插入新记录。但是我确实在加载页面中插入了这个
Private Sub NewCustomerPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Me.CustomerTableAdapter.Fill(Me.AccountingdbDataSet.Customer)
Catch ex As Exception
MessageBox.Show("The database file is unavailable", "Database Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Me.Close()
End Try
End Sub
我不知道我哪里出错了,我迫切需要帮助。
提前致谢!
答案 0 :(得分:0)
尝试使用
Me.CustomerTableAdapter.Flush()