How to ask the user if he wants to consult through messagebuttons

0

I have this code to perform a search of my database and fill out a textbox the code works but I will tell you in the code that I will read that in a catch block that shows me some messagebuttons YesNo and that it will perform certain actions happens that if it does not read simply what it does is that it does not execute the search, it does not give an error, it does not stop the program or show the messagebuttons it just does not do anything, but if I enter a code that exists, it works correctly.

Private Sub btnbuscar2_Click(sender As Object, e As EventArgs) Handles btnbuscar2.Click
    Try
        Dim con As New SqlClient.SqlConnection("Data Source=SAMUELSUSANA; Initial catalog=poovb;integrated security=true")
        Dim cmd As New SqlClient.SqlCommand("select * from tblsuplidores where Suplidor_ID='" & txtcodigosuplidor.Text & "'", con)
        D_table = New DataTable
        con.Open()

        If (txtcodigosuplidor.Text = "") Then
            MessageBox.Show("Debe Pasar El Parametro Del Codigo del Suplidor Para Realizar La Busqueda.", "Hola, Disculpa", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtcodigosuplidor.Focus()

        Else
            dr = cmd.ExecuteReader
            If (dr.Read) Then
                txtsuplidor.Text = dr("Nombre").ToString
                txtcantidad.Enabled = True
                txtcodigosuplidor.Enabled = False
                btnbuscar2.Enabled = False
                txtcantidad.Focus()
            Else
            End If
        End If
        con.Close()
        dr.Close()

    Catch ex As Exception
        Dim Respuesta As DialogResult
        Respuesta = MessageBox.Show("Desea Consultar Por un Suplidor?", "Busqueda Erronea.", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
        If (Respuesta = DialogResult.Yes) Then
            Consultasuplidores.Show()
        Else
            txtcodigosuplidor.Clear()
            txtcodigosuplidor.Focus()
        End If

    End Try

End Sub
    
asked by Samuel Ignacio Susana Confesor 03.08.2017 в 16:45
source

1 answer

0

Explaining the code you made (you only have one logic error):

When doing dr.Read , you are consulting the first record of the result of your query and it will be true in case you get a record, in case you do not find any result it will be false .

I would only go into the exception if there was a execution error inside the Try block, but you do not have any errors.

To consider it would only be necessary:

If (dr.Read) Then
    txtsuplidor.Text = dr("Nombre").ToString
    txtcantidad.Enabled = True
    txtcodigosuplidor.Enabled = False
    btnbuscar2.Enabled = False
    txtcantidad.Focus()
Else            
    Dim Respuesta As DialogResult
    Respuesta = MessageBox.Show("Desea Consultar Por un Suplidor?", "Busqueda Erronea.", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
    If (Respuesta = DialogResult.Yes) Then
        Consultasuplidores.Show()
    Else
        txtcodigosuplidor.Clear()
        txtcodigosuplidor.Focus()
    End If                              
End If
    
answered by 03.08.2017 в 17:48