输入框取消确定和X按钮与Try&在VB.NET中捕获异常处理

时间:2015-11-25 17:48:07

标签: vb.net visual-studio try-catch inputbox

我已经编写了一个代码,用于从输入框中删除用户输入选项,其余代码没问题,但是当用户取消或按下OK(无值插入)或按X仍然是Try& Catch异常显示从STRING类型到Decimal的转换无效。 所以我使用下面的代码,但仍然失败了!我想知道是否有人可以帮助我!

我也试图从这个答案中得到帮助但仍然如此 VB.NET Inputbox - How to identify when the Cancel Button is pressed? 到目前为止,这是我的代码:

Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
    Try
        Dim isbn As Decimal = InputBox("Enter Book ISBN", "Delete")


    If ISBN = " " Then
        MessageBox.Show("You must enter an ISBN to continue.")
        Exit Sub
    ElseIf StatusDate = "" Then
        Exit Sub
    End If

        'Below command is performed with the help of SQL stored prodecures
        cmd = New SqlCommand("IF EXISTS(SELECT * FROM book WHERE isbn = @isbn) " _
& " BEGIN " _
& " delete from published_by where isbn = @isbn; " _
& " delete from book_return where isbn = @isbn; " _
& " delete from memberbook_issue where isbn = @isbn; " _
& " delete from book where isbn = @isbn;" _
& " SELECT 1; " _
& " END " _
& " ELSE SELECT 0", cn)
        cmd.Parameters.Add(New SqlParameter("@isbn", SqlDbType.Decimal, 13) With {.Value = isbn})

        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If
        Dim returnValue As Integer = CInt(cmd.ExecuteScalar())
        If returnValue = 1 Then
            MessageBox.Show("Record Successfully Deleted from current table & dependant table(s)", _
                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("No Book record with provided ISBN", "Information", _
                            MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        da = New SqlDataAdapter("select b.staff_id 'Staff ID', b.pub_id 'Publisher ID', b.sub_code 'Subject Code', " _
                                & " b.isbn 'ISBN', b.book_name 'Book Name', b.author 'Author', b.price 'Price', " _
                                & " b.rack_no 'Rack No#', b.no_of_books 'No# of Books', pby.vol_no 'Volume No#', " _
                                & " pby.pub_date 'Publish Date' from book b join published_by pby " _
                                & " on b.isbn = pby.isbn", cn)
        dt = New DataTable
        da.Fill(dt)
        dgvbook.DataSource = dt
    Catch ex As Exception
        MessageBox.Show("Not Completed Because OF The Following Error " & "%" & ex.Message & "%", "Error", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

1 个答案:

答案 0 :(得分:1)

如果我必须在输入框中使用,我将如何处理此问题:

Dim sResult as String = ""
Dim dResult as Decimal
Do    
    sResult = InputBox("Enter Book ISBN", "Delete")
    If Not Decimal.TryParse(sResult, dResult)
        Messagebox.show("...")
    Else
        Exit Loop
    End If
Loop