Visual Basic.NET - Error en busqueda

 
Vista:

Error en busqueda

Publicado por Laura (8 intervenciones) el 16/05/2007 03:08:25
Hola!!
Tengo una pequeña funcion buscar en una clase "Cliente" y me marca un error en la sentencia "rs = Conectar.EjecutarConsulta(cmd)" El error que me aparece dice: "Se produjo una excepción en el inicializador de tipo de 'BUSCAR.Conectar'."

¿¿alguen tiene idea de proque puede ser??

El codigo de la funcion es este:

Public Function BuscarCliente(ByVal DNI As Integer) As List(Of Cliente)

Dim cmd As New SqlCommand
cmd.CommandText = "Select [DNI], [Nombre], [Apellido] from [Cliente] where DNI=@DNI"
cmd.CommandType = CommandType.Text

Dim prm As New SqlParameter
prm.ParameterName = "@DNI"
prm.Direction = ParameterDirection.Input
prm.Value = Me.DNI
cmd.Parameters.Add(prm)

Dim rs As SqlDataReader
rs = Conectar.EjecutarConsulta(cmd) '<--------ACA ERROR

Dim r As Cliente
r = New Cliente()
r.Nombre = rs("Nombre")
r.Apellido = rs("Apellido")

End Function
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Error en busqueda

Publicado por Harold V (411 intervenciones) el 16/05/2007 09:26:35
Revisa bien este codigo, funciona bien

Supongo Conectar es tu conexion a la BD..


Public Function BuscarCliente(ByVal DNI As Integer) As List(Of Cliente)

Dim lista As New List(Of Cliente)
Dim cmd As New SqlCommand

Try
conectar.Open()
cmd.Connection = conectar

cmd.CommandText = "Select DNI,Nombre,Apellido from Clientes where DNI=@DNI"
cmd.CommandType = CommandType.Text

Dim prm As New SqlParameter
prm.ParameterName = "@DNI"
prm.Direction = ParameterDirection.Input
prm.Value = DNI
cmd.Parameters.Add(prm)

Dim rs As SqlDataReader
rs = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

While rs.Read
Dim r As Cliente
r = New Cliente()
r.nombre = rs("Nombre")
r.Direccion = rs("Apellido")
lista.Add(r)
End While

Catch ex As SqlException
'Aqui va tu excepcion SQL
Finally
conectar.Close()
End Try

'Probamos que funciona mostrando el nombre del cliente OKKKKKKKKK!!!
'MessageBox.Show(lista.Item(0).Nombre)

Return lista

End Function
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Error en busqueda

Publicado por Toni (47 intervenciones) el 16/05/2007 09:29:30
dr("NomComplert") = Campo de la Base de datos a buscar
txtcerca.Text = texto a buscar en la base de datos
cmdfind = Boton buscar

'Creca
Private Sub cmdfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdfind.Click
Dim trobat As Boolean = False
Dim uf As Integer = dt.Rows.Count - 1
If fila < 0 OrElse uf < 0 Then Exit Sub
If txtcercarcanvi Then
fila = 0
Else
fila = fila + 1
End If
'Mentres no trobat i no final taula,
While (fila <= uf And trobat = False)
Dim dr As DataRow = dt.Rows(fila)
If InStr(1, dr("NomComplert").ToString, txtcerca.Text, CompareMethod.Text) Then
trobat = True
Else
fila = fila + 1
End If
End While

If (trobat = False) Then
'Si elemnent no existeix
If txtcercarcanvi Then
'Si s'ha arribat al final del fitxer
MessageBox.Show("No s'ha trobat cap resultat per aquesta cerca", "Control Horari Personal", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show("No hi han més empleats per aquest cerca", "Control Horari Personal", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
mostrarDatos(fila)
End If
'Si es la primera vegada que entrem canviem el nom del botó
If txtcercarcanvi Then
txtcercarcanvi = False
cmdfind.Text = "&Cerca Següent"
End If
End Sub

'Canvia el nom del boto
Private Sub txtcerca_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcerca.TextChanged
txtcercarcanvi = True
cmdfind.Text = "&Cerca"
End Sub

'Tecla retunr
Private Sub txtcerca_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtcerca.KeyPress
If Asc(e.KeyChar) = 13 Then
cmdfind_Click(sender, e)
End If
End Sub
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar