保存数据库中的数据时出错

时间:2015-01-11 12:36:37

标签: database vb.net

我使用此代码将值保存到访问数据库

我做了什么。

  • 将数据库添加到项目
  • 将数据绑定绑定到文本框
  • 在表单加载时,我得到了从数据库到文本框的值(所以我做得正确)
  • 在按钮1上,我使用此代码添加新字段

代码:

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles    Button2.Click
               TabelaBindingSource.AddNew()
           End Sub

在按钮保存上添加此代码

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Try
        Validate()
        TabelaBindingSource.EndEdit()
         TabelaTableAdapter.Update(EUUsersDataSet.tabela)
        MessageBox.Show("Saved")
    Catch ex As Exception
        MessageBox.Show("Error during saving")
    End Try
End Sub

弹出我的盒子"已保存"但数据库中没有记录我做错了什么?

我尝试通过TabelaBindingNavigator添加它,即使我按下该按钮仍然不会保存它。

1 个答案:

答案 0 :(得分:0)

此代码适用于我:

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.TabelaTableAdapter.Fill(Me.Database1DataSet.Table1)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.TabelaBindingSource.AddNew()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Try
        Me.Validate()
        Me.TabelaBindingSource.EndEdit()
        Me.TabelaAdapterManager.UpdateAll(Me.Database1DataSet)
    Catch ex As Exception
        MessageBox.Show("Error during saving")
    End Try
End Sub