Unable to delete

Qbert

Active member
Joined
Jun 11, 2007
Messages
33
Location
Minnesota
Programming Experience
Beginner
i have a form that has a delete button on it. and when you click the delete button a message box pops up and asks you if you really want to delete it. but no matter what button you press (yes or no). it always acts as if you pressed no.

VB.NET:
If ClientListListBox.SelectedIndex = -1 Then
            'do nothing
        Else
            PassedClientID = ClientIDArray(ClientListListBox.SelectedIndex)
            PassedClientName = ClientNameArray(ClientListListBox.SelectedIndex)

            MsgBox("Delete Client """ & PassedClientName & """?", MsgBoxStyle.YesNo)

            If MsgBoxResult.No Then

                MsgBox("Client """ & PassedClientName & """ was not deleted.", MsgBoxStyle.OkOnly)

            ElseIf MsgBoxResult.Yes Then

                IanConnect.Open()

                Dim DeleteClient As New System.Data.OracleClient.OracleCommand
                DeleteClient.Connection = IanConnect
                DeleteClient.CommandText = "DELETE from CLIENTS where CLIENT_ID = " & PassedClientID

                Delete = DeleteClient.ExecuteNonQuery()

                If Delete > 0 Then
                    MsgBox("Client " & PassedClientName & "  Deleted")
                    Me.Refresh()
                Else
                    MsgBox("No")
                End If

                IanConnect.Close()

            End If
        End If

can anyone see a problem?

thanks
 
MsgBox() is a function method that return different MsgboxResult values. Compare with this value. For example:
If MsgBox(...) = MsgBoxResult.Ok Then
 
isnt that what this is doing?

If MsgBoxResult.No Then

saying that if the result of the message box is no then do something?

and i cant do something like this. i get an error.

If MsgBox() = MsgBoxResult.Yes Then
 
MsgBoxResult.No is a constant value, it is not the return value from your method call.

What error is that you get? "argument not specified"?
 
when i try something like this

VB.NET:
If MsgBox() = MsgBoxResult.No Then

i get this error. i have tried many different combinations. i have used the same code on a different application and it worked.


Error 1 Argument not specified for parameter 'Prompt' of 'Public Function MsgBox(Prompt As Object, [Buttons As Microsoft.VisualBasic.MsgBoxStyle = Microsoft.VisualBasic.MsgBoxStyle.OkOnly], [Title As Object = Nothing]) As Microsoft.VisualBasic.MsgBoxResult'.
 
You had specified arguments here: MsgBox("Delete Client """ & PassedClientName & """?", MsgBoxStyle.YesNo)
Why is it suddenly a problem for you to specify the arguments that the Msgbox function needs?
 
i guess i am not sure what arguments i have to specify as i have done it in the past like the example above and it was working. and i am not sure why it is not working now.
 
i did this and it works now.

thanks

VB.NET:
Dim YesNoDelete As MsgBoxResult = MsgBox("Are you sure you want to delete the client """ & PassedClientName & """?", MsgBoxStyle.YesNo)

            If YesNoDelete = MsgBoxResult.No Then
 
You don't have to store the return value in a variable before you make the comparison, unless you need to compare several times or find it more readable.
 
that is just an example that i found on the internet that works. i dont know much about vb and i am just learning as i go along.

is there a way that you would recommend doing things?

thanks
 
Yes, the first suggestion is usually sufficient for most these cases:
If MsgBox(................................) = MsgBoxResult.yes Then
 
That's the "spoonfeed" notation, it's where you fill in the arguments for the function, MsgBox function does require that you specify at least the Prompt argument, the other arguments are optional.
 
I've corrected your code. Dont use MsgBox; it's old school. Dont use empty if clauses, turn the logic around.

VB.NET:
        If ClientListListBox.SelectedIndex <> -1 Then
            PassedClientID = ClientIDArray(ClientListListBox.SelectedIndex)
            PassedClientName = ClientNameArray(ClientListListBox.SelectedIndex)

            Dim answer as MessageBoxResult

            answer = MessageBox.Show(String.Format("Delete Client ""{0}""?", PassedClientName), "Confirm Delete", MessageBoxButtons.YesNo)

            If answer = DialogResult.No Then

                MessageBox.Show("Client was not deleted")

            ElseIf answer = DialogResult.Yes Then

                IanConnect.Open()

                Dim DeleteClient As New System.Data.OracleClient.OracleCommand
                DeleteClient.Connection = IanConnect

                [B]'ya, dont do database queries like this. Take a read of the PQ link in my signature
                'also, you wont need to do this when you learn the new ways of data access[/B]
                DeleteClient.CommandText = "DELETE from CLIENTS where CLIENT_ID = " & PassedClientID

                Delete = DeleteClient.ExecuteNonQuery()

                If Delete > 0 Then Me.Refresh()

                IanConnect.Close()

            End If
        End If


Also, dont bombard your user with MessageBoxes, they become highly irritating because they stop progress, and have to be responded to. Additionally, if your user leaves a box open for a long time, and your IanConnect times out, youll get a nice error when you try to call Close() and it might just kill your app back to the desktop.

Never, ever, ever do any long running or blocking op with a database connection open. Open it, do your stuff, close it, then mess with the long ops. This wont be an issue when you start doing data access "properly" -> microsoft's (good) code handles all the database donkey work and prevents you littering your button handlers with code that doesnt belong.
 
Back
Top