Visual Basic.NET - Contar registros con condición de una tabla.

 
Vista:

Contar registros con condición de una tabla.

Publicado por Juan (2 intervenciones) el 21/03/2013 16:06:18
¡Hola a todos! Estoy siguiendo un ejemplo de Visual por capas. Y lo que es mostrar, insertar, modificar y borrar me lo hace perfectamente pero cuando quiero contar operarios activos de una tabla no me hace nada. No sé que estoy haciendo mal. Les dejo lo que estoy haciendo.

La sql es = SELECT COUNT (*) AS ['Operarios Activos']
FROM Operario
WHERE Activo = True;

Capa Negocio


Public Class Negoperarios
Dim datos As Conexion
Sub New()
datos = New Conexion
End Sub
#Region "Propertys"
Private cuentaactivos As Integer
Public Property Ccuentaactivos() As Integer
Get
Return cuentaactivos
End Get
Set(ByVal value As Integer)
cuentaactivos = value
End Set
End Property

Private registro As Integer
Public Property Cregistro() As Integer
Get
Return registro
End Get
Set(ByVal value As Integer)
registro = value
End Set
End Property

Private nombre As String
Public Property Cnombre() As String
Get
Return nombre
End Get
Set(ByVal value As String)
nombre = value
End Set
End Property
Private apellidos As String
Public Property Capellidos() As String
Get
Return apellidos
End Get
Set(ByVal value As String)
apellidos = value
End Set
End Property
Private nif As String
Public Property Cnif() As String
Get
Return nif
End Get
Set(ByVal value As String)
nif = value
End Set
End Property
Private direccion As String
Public Property Cdireccion() As String
Get
Return direccion
End Get
Set(ByVal value As String)
direccion = value
End Set
End Property
Private localidad As String
Public Property Clocalidad As String
Get
Return localidad
End Get
Set(ByVal value As String)
localidad = value
End Set
End Property
Private provincia As String
Public Property Cprovincia() As String
Get
Return provincia
End Get
Set(ByVal value As String)
provincia = value
End Set
End Property
Private fechaalta As Date
Public Property Cfechaalta() As Date
Get
Return fechaalta
End Get
Set(ByVal value As Date)
fechaalta = value
End Set
End Property
Private activo As Boolean
Public Property Cactivo() As Boolean
Get
Return activo
End Get
Set(ByVal value As Boolean)
activo = value
End Set
End Property
Private fechabaja As Date
Public Property Cfechabaja() As Date
Get
Return fechabaja
End Get
Set(ByVal value As Date)
fechabaja = value
End Set
End Property

#End Region

Function Devolver_Tabla()
Try
Return datos.Devolver_tabla("select * from Operario", "operario")
Catch ex As Exception
Throw New ApplicationException("Error Intentando conectar a Operario")
End Try

End Function



Sub anadir_operario()
'Altas directamente en la base de datos sin tener que sincronizar, recuerda que para ello debemos tener la conección abierta
Try

Dim consulta As String
'Dim cregistro As Integer
consulta = "INSERT INTO operario (`Nombre`, `Apellidos`, `NIF`, `Direccion`, `Localidad`, `Provincia`,`FechaAlta`)"
consulta += " values('" & Cnombre & "','" & Capellidos & "','" & Cnif & "','" & Cdireccion & "','" & Clocalidad & "','" & Cprovincia & "','" & Cfechaalta & "')"
datos.Ejecuta_Comando(consulta)
Catch ex As Exception
' subimos el error para mostrarlo en vista

Throw New ApplicationException(ex.Message & " En el proceso de Modificaciones")
End Try
End Sub
Sub Modificar_propietario()
Try
'datos.Ejecuta_Comando("update operarios set nombre = '" & Cnombre & "' , apellidos = '" & Capellidos & "', NIF = '" & Cnif & "',direccion = '" & Cdireccion & "' , localidad = '" & Clocalidad & "' , provincia = '" & Cprovincia & "',fechaalta = '" & Cfechaalta & "',fechabaja = '" & Cfechabaja & "',Activo = '" & IIf(Cactivo = False, 0, 1) & "'where registro = '" & Cregistro & "'")
datos.Ejecuta_Comando("update operario set nombre = '" & Cnombre & "' , apellidos = '" & Capellidos & "', direccion = '" & Cdireccion & "' , localidad = '" & Clocalidad & "' , provincia = '" & Cprovincia & "' where NIF = '" & Cnif & "'")

Catch ex As Exception
Throw New ApplicationException(ex.Message & " En el proceso de modificaciones")
End Try
End Sub
Sub Borrar_operario()

Try
datos.Ejecuta_Comando("delete from operario where nif='" & Cnif & "'")
Catch ex As Exception
Throw New ApplicationException(ex.Message & " En el proceso de Baja")
End Try
End Sub

'Function Co
ntar_Activos()
' Try
' Return datos.Devolver_tabla("SELECT COUNT (*) AS ['a']FROM(Operario)WHERE Activo = True")
' Catch ex As Exception
' Throw New ApplicationException("Error Intentando conectar a Operario")
' End Try
'End Function

End Class

'Capa Vista
Public Class Voperarios
Dim cloperario As Negoperarios
Private Sub Vpropietarios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cloperario = New NegOperarios
'Label11.Text = cloperario.Contar_Activos
Try
'Método creado para darle formato al DatagridView1.
Me.FormatearGrid()



Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub FormatearGrid()
With Me.DataGridOperarios
'.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
.DataSource = cloperario.Devolver_Tabla
.ReadOnly = True
.Columns("Registro").Width = 50
.Columns("Nombre").Width = 100
.Columns("Apellidos").Width = 200
.Columns("NIF").Width = 80
.Columns("Direccion").Width = 200
.Columns("Direccion").HeaderText = "Dirección"
.Columns("Activo").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
End With
End Sub
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