La aplicación la estoy realizando en Capas y uso procedimientos almacenados:
Aqui la tabla:
Código SQL:
Ver original
CREATE TABLE lugar_libreta_topograf( codlugar INT IDENTITY PRIMARY KEY, nombre VARCHAR(80), descrilugar text ); GO CREATE TABLE libreta_topograf( codigo INT IDENTITY PRIMARY KEY, codigochar VARCHAR(10), descripcion text, tipo_obra VARCHAR(180), sector VARCHAR(100) NOT NULL, anno datetime NOT NULL, codlugar INT CONSTRAINT fk_codlugar FOREIGN KEY(codlugar) REFERENCES lugar_libreta_topograf(codlugar) ); GO
Aqui el procedimiento buscar por año
Código SQL:
Ver original
CREATE PROCEDURE likeanno_dat_lib_topograf @nanno INT, @msg AS VARCHAR(100) OUTPUT AS BEGIN SET NOCOUNT ON; BEGIN Tran likeanno BEGIN Try SELECT lt.codigo,lt.codigochar, lt.descripcion,lt.tipo_obra,lt.sector,lu.nombre,lt.anno FROM libreta_topograf lt INNER JOIN lugar_libreta_topograf lu ON lu.codlugar=lt.codlugar WHERE datepart(YEAR,lt.anno)= @nanno IF @@ROWCOUNT > 0 BEGIN SET @msg = 'Si existen registros.' END ELSE BEGIN SET @msg = 'No existe registro.' END COMMIT TRAN likeanno END try BEGIN Catch SET @msg = 'Ocurrio un Error: ' + ERROR_MESSAGE() + ' en la línea ' + CONVERT(NVARCHAR(255), ERROR_LINE() ) + '.' ROLLBACK TRAN likeanno END Catch END GO
APLICACION
CAPA EntidadNegocio
--> Class LibretaTopografEN.vb
Código vb:
Ver original
Public Class LibretaTopografEN inherits LugarLibretaTopografEN Private gcodigo As Integer Private gcodigochar As String Private gdescripcion As String Private gtipo_obra As String Private gsector As String Private gcodlugar As Integer 'private gnombre as String Private ganno As Date '---------------------------------- Public Property codigo() As Integer Get return gcodigo End Get Set(ByVal value As Integer) gcodigo=value End Set End Property '---------------------------------- '---------------------------------- Public Property codigochar() As String Get return gcodigochar End Get Set(ByVal value As String) gcodigochar=value End Set End Property '---------------------------------- '---------------------------------- Public Property descripcion() As String Get return gdescripcion End Get Set(ByVal value As String) gdescripcion=value End Set End Property '---------------------------------- '---------------------------------- Public Property tipo_obra() As String Get return gtipo_obra End Get Set(ByVal value As String) gtipo_obra=value End Set End Property '---------------------------------- '---------------------------------- Public Property sector() As String Get return gsector End Get Set(ByVal value As String) gsector=value End Set End Property '---------------------------------- '---------------------------------- Public Property codlugar() As Integer Get return gcodlugar End Get Set(ByVal value As Integer) gcodlugar=value End Set End Property '---------------------------------- '---------------------------------- Public Property anno() As Date Get return ganno End Get Set(ByVal value As Date) ganno=value End Set End Property '---------------------------------- End Class
--> Class LugarLibretaTopografEN.vb
Código vb:
Ver original
Public Class LugarLibretaTopografEN Private gcodlugar As Integer Private gnombre As String Private gdescrilugar As String '---------------------------------- Public Property codlugar() As Integer Get return gcodlugar End Get Set(ByVal value As Integer) gcodlugar=value End Set End Property '---------------------------------- '---------------------------------- Public Property nombre() As String Get return gnombre End Get Set(ByVal value As String) gnombre=value End Set End Property '---------------------------------- '---------------------------------- Public Property descrilugar() As String Get return gdescrilugar End Get Set(ByVal value As String) gdescrilugar=value End Set End Property '---------------------------------- End Class
CAPA AccesoDatos
-->LibretaTopografAD.vb
Código vb:
Ver original
Imports System.Data.SqlClient Imports System.Data.SqlTypes Imports EntidadNegocio Public Class LibretaTopografAD Private conn As New SqlConnection Private comando As New SqlCommand #Region "Cadena de conexion" Public Sub New() Dim Objconexion As New ConexionAD conn = Objconexion.abrir comando.Connection=conn End Sub #End Region #Region "Función convertir datos" Private Shared Function Convertirdatos(ByVal reader As IDataReader) As LibretaTopografEN Dim libreta As New LibretaTopografEN libreta.codigo = LTrim(RTrim(Convert.ToString(reader(0)))) libreta.codigochar=LTrim(RTrim(Convert.ToString(reader(1)))) libreta.descripcion = LTrim(RTrim(Convert.ToString(reader(2)))) libreta.tipo_obra = LTrim(RTrim(Convert.ToString(reader(3)))) libreta.sector = LTrim(RTrim(Convert.ToString(reader(4)))) libreta.nombre = LTrim(RTrim(Convert.ToString(reader(5)))) libreta.anno = LTrim(RTrim(convert.ToDateTime(reader(6)))) Return libreta End Function #End Region #Region "Función Buscar por Año" Public Function buscarporanno(ByVal ObjLibretaTopografEN As LibretaTopografEN)As List(Of LibretaTopografEN) Dim list As New List(Of LibretaTopografEN) Dim reader As SqlDataReader Try Comando.CommandType=CommandType.StoredProcedure comando.CommandText="likeanno_dat_lib_topograf" Dim _anno As New SqlParameter("@nanno",SqlDbType.Int) _anno.Value=ObjLibretaTopografEN.anno _anno.Direction=ParameterDirection.Input comando.Parameters.Add(_anno) Dim _msg As New SqlParameter("@msg",SqlDbType.VarChar,100) _msg.Direction=ParameterDirection.Output comando.Parameters.Add(_msg) reader=comando.ExecuteReader() ' Dim mensaje As String ' mensaje=Convert.ToString(_msg.Value) ' MsgBox(mensaje) While reader.Read Dim l As LibretaTopografEN=New LibretaTopografEN list.Add(Convertirdatos(reader)) End While Return list Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical,ex.Source) Finally conn.Close() conn.ClearAllPools() conn=Nothing End Try End Function #End Region End Class
CAPA LOGICA DEL NEGOCIO
-->Clase LibretaTopografLN.vb
Código vb:
Ver original
Imports AccesoDatos imports EntidadNegocio Public Class LibretaTopografLN private ObjLibretaTopografAD as LibretaTopografAD Public Sub New() ObjLibretaTopografAD= New LibretaTopografAD End Sub #Region "Establecer conexion con la función Buscar por Año" Public Function buscarporanno(ByVal ObjLibretaTopografEN As LibretaTopografEN) As List(Of LibretaTopografEN) return ObjLibretaTopografAD.buscarporanno(ObjLibretaTopografEN) End Function #End Region
CAPA PRESENTACIÓN
-->ListarLibretaTopografica.vb
Código vb:
Ver original
Imports EntidadNegocio Imports LogicaNegocio Public Partial Class ListarLibretaTopografica Public Sub New() ' The Me.InitializeComponent call is required for Windows Forms designer support. Me.InitializeComponent() ' ' TODO : Add constructor code after InitializeComponents ' End Sub Private ObjLibretaTopografEN As LibretaTopografEN Private ObjLibretaTopografLN As LibretaTopografLN [B]'AQUI ESTA EL BOTON BUSCAR[/B] Sub BtnbuscarClick(sender As Object, e As EventArgs) If(txtbuscar.Text<>"")Then If(rbsector.Checked)Then Try 'rbAnno.Checked=False ObjLibretaTopografEN=New LibretaTopografEN ObjLibretaTopografLN=New LibretaTopografLN ObjLibretaTopografEN.sector=LTrim(RTrim(txtbuscar.Text)) dgvlistlibretatopo.AutoGenerateColumns=False formato() dgvlistlibretatopo.DataSource= ObjLibretaTopografLN.buscarporsector(ObjLibretaTopografEN) Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical,ex.Source) End Try End If If(rbAnno.Checked)Then Try rbsector.Checked=False ObjLibretaTopografEN=New LibretaTopografEN ObjLibretaTopografLN=New LibretaTopografLN Dim a As Integer a=txtbuscar.Text ObjLibretaTopografEN.anno=LTrim(RTrim(a)) dgvlistlibretatopo.AutoGenerateColumns=False formato() dgvlistlibretatopo.DataSource= ObjLibretaTopografLN.buscarporanno(ObjLibretaTopografEN) Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical,ex.Source) End Try End If End If End Sub End Class
Y EL ERROR QUE ME SALTA ES
La conversion de la cadena "2013" en el tipo date no es valida