【发布时间】:2015-11-25 17:48:07
【问题描述】:
我已经编写了一个代码来从输入框中删除用户输入选择,其余代码都可以,但是当用户取消或按下 OK(未插入值)或按下 X 时,Try & Catch 异常显示从类型转换STRING 转十进制无效。 所以我使用下面的代码但仍然失败!我想知道是否有人可以帮助我!
我也试图从这个答案中获得帮助,但它仍然可以 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
【问题讨论】:
-
输入框在哪里?
-
输入框在取消或使用 X 时返回空字符串。测试结果,你应该没有任何问题。
-
@Plutonix 它位于代码部分的上方,但我不知道如何将其归结为代码部分:( 在本网站中
-
@steve 我试着把这个放在我的代码上: If Input = " " Then MessageBox.Show("You must enter an ISBN to continue.") Exit Sub ElseIf ISBN = "" Then Exit Sub结束如果
-
抱歉,未格式化部分没有看到。 InputBox 返回一个字符串; not a Decimal 和 Decimal 对于 ISBN 来说似乎是一个奇怪的选择。无论如何,链接的问题会告诉您该怎么做:测试它并在它为空时
Exit Sub
标签: vb.net visual-studio try-catch inputbox