Inputbox Cancel OK and X button with Try & Catch exception handling in VB.NET
Inputbox Cancel OK and X button with Try & Catch exception handling in VB.NET
我已经编写了一个代码来从输入框中删除用户输入的选择,其余代码都可以,但是当用户取消或按 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
如果我不得不在输入框中使用,我将如何处理:
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
我已经编写了一个代码来从输入框中删除用户输入的选择,其余代码都可以,但是当用户取消或按 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
如果我不得不在输入框中使用,我将如何处理:
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