Vb.net 从数据库获取数据时出现异常

Vb.net 从数据库获取数据时出现异常,vb.net,Vb.net,这是我的密码。调用Quantity而不是Quantity1将抛出错误。例如,在另一种形式中,我只填写quantity1,但其他数量我不填写,然后当我想查找数据时,它会出错。我认为您的问题在于包含Convert.ToInt32(txtQ.Text)的行中的一行,您试图将可能无效的字符串转换为整数。考虑使用替代,这消除了处理异常的需要: Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.Event

这是我的密码。调用Quantity而不是Quantity1将抛出错误。例如,在另一种形式中,我只填写quantity1,但其他数量我不填写,然后当我想查找数据时,它会出错。

我认为您的问题在于包含
Convert.ToInt32(txtQ.Text)
的行中的一行,您试图将可能无效的字符串转换为整数。考虑使用替代,这消除了处理异常的需要:

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

我认为您的问题在于其中一行包含
Convert.ToInt32(txtQ.Text)
,您试图将可能无效的字符串转换为整数。考虑使用替代,这消除了处理异常的需要:

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

错误说明了什么?你读了吗?txtQ1.AUTOCOMPLETECUSTOURCE=data(“Quantity1”)部分出错?@SLaks会这样说:“输入字符串格式不正确”。@matzone我刚刚编辑了新代码。请学习使用调试器。另外,请学习在您的问题中包含信息。“它会出错”对这里的任何人都没有意义,除了你,因为你没有告诉我们“错误”是什么意思。您需要明确,当您收到错误时,告诉我们错误是什么,并包括您收到的确切错误消息。错误说明了什么?你读了吗?txtQ1.AUTOCOMPLETECUSTOURCE=data(“Quantity1”)部分出错?@SLaks会这样说:“输入字符串格式不正确”。@matzone我刚刚编辑了新代码。请学习使用调试器。另外,请学习在您的问题中包含信息。“它会出错”对这里的任何人都没有意义,除了你,因为你没有告诉我们“错误”是什么意思。您需要明确,当您收到错误时,请告诉我们该错误是什么,并包括您收到的确切错误消息。