从数据库中获取数据时出现异常

时间:2013-05-17 18:35:11

标签: vb.net

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim data As DataRow

        Try
            OleDbDataAdapter1.Fill(Me.DataSet11.PAYMENT)
            data = DataSet11.PAYMENT.Rows.Find(txtpayment.Text)
            txtpayment.Text = data("CustomerIC")
            txtName.Text = data("CustomerName")
            txtadd.Text = data("CustomerAddress")
            txttel.Text = data("NoTel")
            If cKurung.AutoCheck = True Then
                cKurung.Checked = data("Baju Kurung")
                Quan1 = Convert.ToInt32(txtQ1.Text)
                Quan1 = data("Quantity1")
            End If

            If cKebaya.AutoCheck = True Then
                cKebaya.Checked = data("Baju Kebaya")
                Quan2 = Convert.ToInt32(txtQ2.Text)
                Quan2 = data("Quantity2")
            End If

            If cTudung.AutoCheck = True Then
                cTudung.Checked = data("Tudung")
                Quan3 = Convert.ToInt32(txtQ3.Text)
                Quan3 = data("Quantity3")
            End If

            If cSelendang.AutoCheck = True Then
                cSelendang.Checked = data("Selendang")
                Quan4 = Convert.ToInt32(txtQ4.Text)
                Quan4 = data("Quantity4")
            End If

            If cTelekung.AutoCheck = True Then
                cTelekung.Checked = data("Telekung")
                Quan5 = Convert.ToInt32(txtQ5.Text)
                Quan5 = data("Quantity5")
            End If

            If cAnakTudung.AutoCheck = True Then
                cAnakTudung.Checked = data("Anak Tudung")
                Quan6 = Convert.ToInt32(txtQ6.Text)
                Quan6 = data("Quantity6")
            End If
            txtQuan.Text = data("Quantity")
            txtPrice.Text = data("Price")

        Catch ex As Exception
            MessageBox.Show("Invalid Customer IC", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtpayment.Text = ""
            txtpayment.Focus()
            txtpayment.ReadOnly = False

        End Try
    End Sub

这是我的代码。调用数量而不是数量1会产生错误。例如,在其他形式我只填充数量1但其他数量我不填充然后当我想找到它将错误的数据。

1 个答案:

答案 0 :(得分:1)

我认为您的问题在于包含Convert.ToInt32(txtQ.Text)的那一行,其中您尝试将可能无效的字符串转换为整数。请考虑使用Int32.TryParse,这样就无需处理异常:

Dim result As Int32;

' Returns true if conversion was successful
If Int32.TryParse(txtQ1.Text, out result)  Then 
      Quan1 = result;
End If