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_SelectedIndexCh anged(send er As Object, e As EventArgs) Handles lstNombres.SelectedIndexCh anged
Try
Dim cRegistro As String = lstNombres.SelectedItem
Dim cClave As String = Trim(Microsoft.VisualBasic .Right(cRe gistro, 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("LanRecib ido")
txtNombre.Text = Reader.GetString("Nombre")
txtPaterno.Text = Reader.GetString("Paterno" )
txtMaterno.Text = Reader.GetString("Materno" )
txtNumeroDocumento.Text = Reader.GetString("Numerodo cumento")
txtCiudadTramita.Text = Reader.GetString("Ciudadtr amite")
txtTelefonoCelular.Text = Reader.GetString("Telcelul ar")
txtTelefonoOtro.Text = Reader.GetString("Telotro" )
txtObservaciones.Text = Reader.GetString("Observac iones")
End While
Catch ex As Exception
Console.WriteLine(ex.Messa ge)
End Try
Reader.Close()
Reader.Dispose()
Conn.Close()
Conn.Dispose()
End Sub
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_SelectedIndexCh
Try
Dim cRegistro As String = lstNombres.SelectedItem
Dim cClave As String = Trim(Microsoft.VisualBasic
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("LanRecib
txtNombre.Text = Reader.GetString("Nombre")
txtPaterno.Text = Reader.GetString("Paterno"
txtMaterno.Text = Reader.GetString("Materno"
txtNumeroDocumento.Text = Reader.GetString("Numerodo
txtCiudadTramita.Text = Reader.GetString("Ciudadtr
txtTelefonoCelular.Text = Reader.GetString("Telcelul
txtTelefonoOtro.Text = Reader.GetString("Telotro"
txtObservaciones.Text = Reader.GetString("Observac
End While
Catch ex As Exception
Console.WriteLine(ex.Messa
End Try
Reader.Close()
Reader.Dispose()
Conn.Close()
Conn.Dispose()
End Sub
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.SqlN ullValueEx ception' occurred in MySql.Data.dll
Data is Null. This method or property cannot be called on Null values.
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.SqlN
Data is Null. This method or property cannot be called on Null values.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
Great Job!!!
Thank you
Thank you
Thank you,
Sorry was working on php code that's where I wrote it. Great it worked for you.
Sorry was working on php code that's where I wrote it. Great it worked for you.
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.