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
elm9999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guru JiCommented:
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.
1
elm9999Author Commented:
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.
0
Guru JiCommented:
You need to check for IsDBNull:

if(!Reader.IsDBNull(1))
{
  txtNumeroLan.Text = Reader.GetString(1);
}
That's your only reliable way to detect and handle this situation and you have to do it for each text box.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

elm9999Author Commented:
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
0
elm9999Author Commented:
Great Job!!!

Thank you
0
Guru JiCommented:
Thank you,

Sorry was working on php code that's where I wrote it. Great it worked for you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.