I have written a code to delete user input choice from input box the rest of the code is OK, but when user cancels or presses the OK (No Value inserted) or presses X still the Try & Catch exception shows conversion from type STRING to Decimal is not valid. So I worked with below code but still failed! I wonder if anyone could help me in this!
I also tried to get help from this answer but still it does VB.NET Inputbox - How to identify when the Cancel Button is pressed? Here is my code so far:
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