Link to home
Start Free TrialLog in
Avatar of Member_2_6479049
Member_2_6479049

asked on

can't filling text boxes from MySql query using vb.net

Hi guys,

A have a form with some text boxes that I need to fill from a MySql query, the problem is that only the first text box is filled and the rest remains empty, I hope you guys can help me please.

    Private Sub lstNombres_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstNombres.SelectedIndexChanged
        Try
            Dim cRegistro As String = lstNombres.SelectedItem
            Dim cClave As String = Trim(Microsoft.VisualBasic.Right(cRegistro, 5))
            Dim nClave As Integer = CInt(cClave)
            Dim cQuery As String

            If Conn.State = ConnectionState.Closed Then
                Conn.Open()
            End If

            cQuery = "SELECT * FROM registros where registros.clave =  '" & nClave & "'"

            Command = New MySqlCommand(cQuery, Conn)
            Reader = Command.ExecuteReader()

            While Reader.Read()
                txtNumeroLan.Text = Reader.GetString("Lan")
                txtLanRecibido.Text = Reader.GetString("LanRecibido")
                txtNombre.Text = Reader.GetString("Nombre")
                txtPaterno.Text = Reader.GetString("Paterno")
                txtMaterno.Text = Reader.GetString("Materno")
                txtNumeroDocumento.Text = Reader.GetString("Numerodocumento")
                txtCiudadTramita.Text = Reader.GetString("Ciudadtramite")
                txtTelefonoCelular.Text = Reader.GetString("Telcelular")
                txtTelefonoOtro.Text = Reader.GetString("Telotro")
                txtObservaciones.Text = Reader.GetString("Observaciones")
            End While
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        Reader.Close()
        Reader.Dispose()
        Conn.Close()
        Conn.Dispose()
    End Sub
Avatar of Guru Ji
Guru Ji
Flag of Canada image

In your while read,

Instead of Reader.GetString("Field Name")

try Reader.GetString(0), Reader.GetString(1), Reader.GetString(2)

0,1,2 are based on the fileds you have in the select statement.
0 is for Lan field
1 is for LanRecibido ... and so on.

Try this and report if that fixes anything because sometimes if field names are not exact or have any space it won't display correct where as specifying the number for that field will display the content regardless of the name.
Avatar of Member_2_6479049
Member_2_6479049

ASKER

No, it doesn't wotk :(

            While Reader.Read()
                txtNumeroLan.Text = Reader.GetString(1)
                txtLanRecibido.Text = Reader.GetString(2)
                txtNombre.Text = Reader.GetString(3)
                txtPaterno.Text = Reader.GetString(4)
                txtMaterno.Text = Reader.GetString(5)
                txtNumeroDocumento.Text = Reader.GetString(8)
                txtCiudadTramita.Text = Reader.GetString(12)
                txtTelefonoCelular.Text = Reader.GetString(13)
                txtTelefonoOtro.Text = Reader.GetString(14)
                txtObservaciones.Text = Reader.GetString(15)
            End While

On the output debug of the coding windows apears this:

A first chance exception of type 'System.Data.SqlTypes.SqlNullValueException' occurred in MySql.Data.dll
Data is Null. This method or property cannot be called on Null values.
ASKER CERTIFIED SOLUTION
Avatar of Guru Ji
Guru Ji
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great Guru !!!

it's done and working good. I only change your last example  you made for PHP :)

If Not (Reader.IsDBNull(1)) Then
      txtNumeroLan.Text = Reader.GetString(1)
End If
Great Job!!!

Thank you
Thank you,

Sorry was working on php code that's where I wrote it. Great it worked for you.