1

我正在尝试验证连接到访问数据库的 vb.net 程序中的文本框和组合框中的数据。

我在下面使用了以下代码,但收到错误消息;

Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click

  ShowAllStock.StockDataTableAdapter.Insert(Me.cboitemtype.Text, Me.TextBox2.Text, Me.cbocompany.Text, Me.TextBox3.Text, Me.TextBox4.Text)
  ShowAllStock.StockDataTableAdapter.Fill(ShowAllStock.Logins1DataSet.StockData)

  If TextBox2.Text = "" Then
    MsgBox("Please Fill In Your Name", MsgBoxStyle.Critical, "Please Fill In Your Name")
  ElseIf TextBox4.Text = "" Then
    MsgBox("Please Fill In the Total Cost Box", MsgBoxStyle.Critical, "Please Enter Cost")
  ElseIf cboitemtype.Text = "" Then
    MsgBox("Please Select Item Type", MsgBoxStyle.Critical, "Please Select Item Type")
  ElseIf cbocompany.Text = "" Then
    MsgBox("Please Select Company Delivered By", MsgBoxStyle.Critical, "Please Select Company Delivered By")
  ElseIf TextBox3.Text = "" Then
    MsgBox("Please Fill In Quantity", MsgBoxStyle.Critical, "Please Fill In Quantity")
  Else
    MsgBox("Records Added Successfully")
    cboitemtype.Text = ""
    TextBox2.Text = ""
    cbocompany.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
  End If
End Sub
4

1 回答 1

0

如果你的代码是正确的,它可能是什么?虽然我还没有检查过。

然而,当您尝试在验证数据之前保存数据时,也许是它导致了您的错误?

首先交换一些代码行,如下所示,然后当您再次运行它时,如果您遇到任何错误,请使用您收到的错误更新您的问题。

Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
  '-> Try to validate the data
  If TextBox2.Text = "" Then
    MsgBox("Please Fill In Your Name", MsgBoxStyle.Critical, "Please Fill In Your Name")
  ElseIf TextBox4.Text = "" Then
    MsgBox("Please Fill In the Total Cost Box", MsgBoxStyle.Critical, "Please Enter Cost")
  ElseIf cboitemtype.Text = "" Then
    MsgBox("Please Select Item Type", MsgBoxStyle.Critical, "Please Select Item Type")
  ElseIf cbocompany.Text = "" Then
    MsgBox("Please Select Company Delivered By", MsgBoxStyle.Critical, "Please Select Company Delivered By")
  ElseIf TextBox3.Text = "" Then
    MsgBox("Please Fill In Quantity", MsgBoxStyle.Critical, "Please Fill In Quantity")
  Else
    '-> Data was found to be valid
    Try
      ShowAllStock.StockDataTableAdapter.Insert(Me.cboitemtype.Text, Me.TextBox2.Text, Me.cbocompany.Text, Me.TextBox3.Text, Me.TextBox4.Text)
      Try
        ShowAllStock.StockDataTableAdapter.Fill(ShowAllStock.Logins1DataSet.StockData)
        MsgBox("Records Added Successfully")
      Catch
        '-> Error occurred FILLING record!
      End Try
    Catch
      '-> Error occurred INSERTING record!
    End Try
    cboitemtype.Text = ""
    TextBox2.Text = ""
    cbocompany.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
  End If
End Sub

基本上更改行将首先进行验证,然后仅尝试保存数据。

于 2013-04-26T17:48:24.343 回答