0

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
Community
  • 1
  • 1
Hashmatullah Noorzai
  • 771
  • 3
  • 12
  • 34
  • 1
    Where is the input box? – Ňɏssa Pøngjǣrdenlarp Nov 25 '15 at 17:51
  • An inputbox returns an empty string when canceled or when the X is used. Test for that as a result and you should not have any issues. – Steve Nov 25 '15 at 17:54
  • @Plutonix it is above the code section but I don't know how bring it down to the code section :( in this website – Hashmatullah Noorzai Nov 25 '15 at 17:56
  • @steve I tried to put this on my code: If Input = " " Then MessageBox.Show("You must enter an ISBN to continue.") Exit Sub ElseIf ISBN = "" Then Exit Sub End If – Hashmatullah Noorzai Nov 25 '15 at 17:58
  • Sorry, didnt see it in the unformatted part. InputBox returns a string; not a Decimal and Decimal seems an odd choice for an ISBN. At any rate the linked question shows you what to do: test it and `Exit Sub` when it is empty – Ňɏssa Pøngjǣrdenlarp Nov 25 '15 at 17:58
  • @Steve I know the above code in my comment should be working fine but I don't exactly know where to put that in my code – Hashmatullah Noorzai Nov 25 '15 at 18:00
  • A better way to code that would be to let them pick from a list of ISBNs. – Ňɏssa Pøngjǣrdenlarp Nov 25 '15 at 18:07
  • @Plutonix I used Decimal for ISBN cause INT can only support up to 10 digits long in vb but I need 13 digits for ISBN and yeah the code link which I shared, I tried to put it below the inputbox in my code, it doesn't work, because when I run and press cancel it still brings exception! – Hashmatullah Noorzai Nov 25 '15 at 18:09
  • Let me just edit my post and add the code from the link I shared @Plutonix So you can tell me that the position I posted is correct or not? – Hashmatullah Noorzai Nov 25 '15 at 18:14
  • A formatted ISBN, `978-3-16-148410-0` is not a number but a string. Users picking rather than typing them would result in fewer errors – Ňɏssa Pøngjǣrdenlarp Nov 25 '15 at 18:14
  • Yeah @Plutonix but I use it like this 1000123456789 you know without the dashes :P but I understand you If you go for string type it will more standard and fewer errors will occur by users – Hashmatullah Noorzai Nov 25 '15 at 18:16
  • You got the answer below *as* an answer complete with how to convert InputBox string to decimal. You sort of miss the point: I would not abuse the user by making them find/type 13 digits to begin with (where are they going to get the number from?) then I would not use the very crude InputBox for it. **No** your code is not correct. Study the answer below – Ňɏssa Pøngjǣrdenlarp Nov 25 '15 at 18:16
  • @Plutonix Librarian is only managing this, it is not for students, he takes ISBNs from books normally all standard books have it! and yeah if I go by your suggestion to let him select from a list of ISBN then how he would insert new books (that has definitely new ISBNs for it) through another VB form(ISBN_List) ? – Hashmatullah Noorzai Nov 25 '15 at 18:32
  • your question is about *deleting* a book, which means The System should already have the ISBN, which means they could pick from a list. Librarians are people too – Ňɏssa Pøngjǣrdenlarp Nov 25 '15 at 18:35
  • haha you are really smart bro actually I was frustrated by all day coding, So didn't thought about this! anyways coding is enough for day I will work on it tomorrow but thanks again for nice suggestions :) – Hashmatullah Noorzai Nov 25 '15 at 18:39
  • Choosing from a list is a good idea, but it depends on how many titles are in the library. Imagine to choose between thousands, all numeric, list items (hopefully sorted). I suggest to provide a system to search for partial matches on ISBN and only then use a list. (but perhaps the only reasonable solution is a search for title or authors) – Steve Nov 25 '15 at 19:04

1 Answers1

1

Here is how I would handle this if I HAD to use in inputbox:

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
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Steve
  • 5,585
  • 2
  • 18
  • 32