vb.net中的Vaildate文本框和组合boex

时间:2013-04-26 13:29:46

标签: database vb.net visual-studio combobox textbox

尝试验证连接到访问数据库的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

1 个答案:

答案 0 :(得分: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

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