Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/03/2013, 19:03
Avatar de junior1920
junior1920
 
Fecha de Ingreso: noviembre-2010
Ubicación: Tumán
Mensajes: 77
Antigüedad: 14 años, 2 meses
Puntos: 1
Exclamación Error al buscar por Año!!!

Hola nuevamente amigos aqui yo pidiendo nuevamente un poco de su ayuda para resolver este error estoy usando Sharpdevelop y sqlserver2005
La aplicación la estoy realizando en Capas y uso procedimientos almacenados:

Aqui la tabla:
Código SQL:
Ver original
  1. CREATE TABLE lugar_libreta_topograf(
  2. codlugar INT IDENTITY PRIMARY KEY,
  3. nombre VARCHAR(80),
  4. descrilugar text
  5. );
  6. GO
  7. CREATE TABLE libreta_topograf(
  8. codigo INT IDENTITY PRIMARY KEY,
  9. codigochar VARCHAR(10),
  10. descripcion text,
  11. tipo_obra VARCHAR(180),
  12. sector VARCHAR(100) NOT NULL,
  13. anno datetime NOT NULL,
  14. codlugar INT
  15. CONSTRAINT fk_codlugar FOREIGN KEY(codlugar)
  16. REFERENCES lugar_libreta_topograf(codlugar)
  17. );
  18. GO

Aqui el procedimiento buscar por año

Código SQL:
Ver original
  1. CREATE PROCEDURE likeanno_dat_lib_topograf
  2.     @nanno INT,
  3.     @msg AS VARCHAR(100) OUTPUT
  4. AS
  5. BEGIN
  6.     SET NOCOUNT ON;
  7.     BEGIN Tran likeanno
  8.     BEGIN Try
  9.         SELECT lt.codigo,lt.codigochar,
  10.         lt.descripcion,lt.tipo_obra,lt.sector,lu.nombre,lt.anno
  11.         FROM libreta_topograf lt INNER JOIN lugar_libreta_topograf lu
  12.         ON lu.codlugar=lt.codlugar
  13.         WHERE datepart(YEAR,lt.anno)= @nanno
  14.         IF @@ROWCOUNT > 0
  15.             BEGIN
  16.                 SET @msg = 'Si existen registros.'
  17.             END
  18.         ELSE
  19.             BEGIN
  20.                 SET @msg = 'No existe registro.'
  21.             END
  22.         COMMIT TRAN likeanno
  23.     END try
  24.     BEGIN Catch
  25.         SET @msg = 'Ocurrio un Error: ' + ERROR_MESSAGE() + ' en la línea ' + CONVERT(NVARCHAR(255), ERROR_LINE() ) + '.'
  26.         ROLLBACK TRAN likeanno
  27.     END Catch
  28. END
  29. GO

APLICACION
CAPA EntidadNegocio
--> Class LibretaTopografEN.vb
Código vb:
Ver original
  1. Public Class LibretaTopografEN
  2.     inherits LugarLibretaTopografEN
  3.     Private gcodigo As Integer
  4.     Private gcodigochar As String
  5.     Private gdescripcion As String
  6.     Private gtipo_obra As String
  7.     Private gsector As String
  8.     Private gcodlugar As Integer
  9.     'private gnombre as String
  10.     Private ganno As Date
  11.    
  12.    
  13.     '----------------------------------
  14.     Public Property codigo() As Integer
  15.         Get
  16.             return gcodigo
  17.         End Get
  18.         Set(ByVal value As Integer)
  19.             gcodigo=value
  20.         End Set
  21.     End Property
  22.     '----------------------------------
  23.     '----------------------------------
  24.     Public Property codigochar() As String
  25.         Get
  26.             return gcodigochar
  27.         End Get
  28.         Set(ByVal value As String)
  29.             gcodigochar=value
  30.         End Set
  31.     End Property
  32.     '----------------------------------
  33.     '----------------------------------
  34.     Public Property descripcion() As String
  35.         Get
  36.             return gdescripcion
  37.         End Get
  38.         Set(ByVal value As String)
  39.             gdescripcion=value
  40.         End Set
  41.     End Property
  42.     '----------------------------------
  43.     '----------------------------------
  44.     Public Property tipo_obra() As String
  45.         Get
  46.             return gtipo_obra
  47.         End Get
  48.         Set(ByVal value As String)
  49.             gtipo_obra=value
  50.         End Set
  51.     End Property
  52.     '----------------------------------
  53.     '----------------------------------
  54.     Public Property sector() As String
  55.         Get
  56.             return gsector
  57.         End Get
  58.         Set(ByVal value As String)
  59.             gsector=value
  60.         End Set
  61.     End Property
  62.     '----------------------------------
  63.     '----------------------------------
  64.     Public Property codlugar() As Integer
  65.         Get
  66.             return gcodlugar
  67.         End Get
  68.         Set(ByVal value As Integer)
  69.             gcodlugar=value
  70.         End Set
  71.     End Property
  72.     '----------------------------------
  73.     '----------------------------------
  74.     Public Property anno() As Date
  75.         Get
  76.             return ganno
  77.         End Get
  78.         Set(ByVal value As Date)
  79.             ganno=value
  80.         End Set
  81.     End Property
  82.     '----------------------------------
  83. End Class
--> Class LugarLibretaTopografEN.vb
Código vb:
Ver original
  1. Public Class LugarLibretaTopografEN
  2.     Private gcodlugar As Integer
  3.     Private gnombre As String
  4.     Private gdescrilugar As String
  5.    
  6.    
  7.     '----------------------------------
  8.     Public Property codlugar() As Integer
  9.         Get
  10.             return gcodlugar
  11.         End Get
  12.         Set(ByVal value As Integer)
  13.             gcodlugar=value
  14.         End Set
  15.     End Property
  16.     '----------------------------------
  17.     '----------------------------------
  18.     Public Property nombre() As String
  19.         Get
  20.             return gnombre
  21.         End Get
  22.         Set(ByVal value As String)
  23.             gnombre=value
  24.         End Set
  25.     End Property
  26.     '----------------------------------
  27.     '----------------------------------
  28.     Public Property descrilugar() As String
  29.         Get
  30.             return gdescrilugar
  31.         End Get
  32.         Set(ByVal value As String)
  33.             gdescrilugar=value
  34.         End Set
  35.     End Property
  36.     '----------------------------------
  37. End Class

CAPA AccesoDatos
-->LibretaTopografAD.vb
Código vb:
Ver original
  1. Imports System.Data.SqlClient
  2. Imports System.Data.SqlTypes
  3. Imports EntidadNegocio
  4. Public Class LibretaTopografAD
  5.     Private conn As New SqlConnection
  6.     Private comando As New SqlCommand
  7.     #Region "Cadena de conexion"
  8.     Public Sub New()
  9.         Dim Objconexion As New ConexionAD
  10.         conn = Objconexion.abrir
  11.         comando.Connection=conn
  12.     End Sub
  13.     #End Region
  14.     #Region "Función convertir datos"
  15.     Private Shared Function Convertirdatos(ByVal reader As IDataReader) As LibretaTopografEN
  16.         Dim libreta As New LibretaTopografEN
  17.  
  18.         libreta.codigo = LTrim(RTrim(Convert.ToString(reader(0))))
  19.         libreta.codigochar=LTrim(RTrim(Convert.ToString(reader(1))))
  20.         libreta.descripcion = LTrim(RTrim(Convert.ToString(reader(2))))
  21.         libreta.tipo_obra = LTrim(RTrim(Convert.ToString(reader(3))))
  22.         libreta.sector = LTrim(RTrim(Convert.ToString(reader(4))))
  23.         libreta.nombre = LTrim(RTrim(Convert.ToString(reader(5))))
  24.         libreta.anno = LTrim(RTrim(convert.ToDateTime(reader(6))))
  25.         Return libreta
  26.     End Function
  27.     #End Region
  28.  #Region "Función Buscar por Año"
  29.     Public Function buscarporanno(ByVal ObjLibretaTopografEN As LibretaTopografEN)As List(Of LibretaTopografEN)
  30.         Dim list As New List(Of LibretaTopografEN)
  31.         Dim reader As SqlDataReader
  32.     Try
  33.             Comando.CommandType=CommandType.StoredProcedure
  34.             comando.CommandText="likeanno_dat_lib_topograf"
  35.             Dim _anno As New SqlParameter("@nanno",SqlDbType.Int)
  36.             _anno.Value=ObjLibretaTopografEN.anno
  37.             _anno.Direction=ParameterDirection.Input
  38.             comando.Parameters.Add(_anno)
  39.            
  40.             Dim _msg As New SqlParameter("@msg",SqlDbType.VarChar,100)
  41.             _msg.Direction=ParameterDirection.Output
  42.             comando.Parameters.Add(_msg)
  43.            
  44.             reader=comando.ExecuteReader()
  45. '           Dim mensaje As String
  46. '           mensaje=Convert.ToString(_msg.Value)
  47. '           MsgBox(mensaje)
  48.             While reader.Read
  49.                 Dim l As LibretaTopografEN=New LibretaTopografEN
  50.                 list.Add(Convertirdatos(reader))
  51.             End While
  52.             Return list
  53.        
  54.     Catch ex As Exception
  55.         MsgBox(ex.Message, MsgBoxStyle.Critical,ex.Source)
  56.     Finally
  57.         conn.Close()
  58.         conn.ClearAllPools()
  59.         conn=Nothing
  60.     End Try
  61.     End Function
  62.     #End Region
  63. End Class

CAPA LOGICA DEL NEGOCIO
-->Clase LibretaTopografLN.vb
Código vb:
Ver original
  1. Imports AccesoDatos
  2. imports EntidadNegocio
  3. Public Class LibretaTopografLN
  4.     private ObjLibretaTopografAD as LibretaTopografAD
  5.     Public Sub New()
  6.         ObjLibretaTopografAD= New LibretaTopografAD
  7.     End Sub
  8. #Region "Establecer conexion con la función Buscar por Año"
  9.     Public Function buscarporanno(ByVal ObjLibretaTopografEN As LibretaTopografEN) As List(Of LibretaTopografEN)
  10.         return ObjLibretaTopografAD.buscarporanno(ObjLibretaTopografEN)    
  11.     End Function
  12.     #End Region

CAPA PRESENTACIÓN
-->ListarLibretaTopografica.vb
Código vb:
Ver original
  1. Imports EntidadNegocio
  2. Imports LogicaNegocio
  3. Public Partial Class ListarLibretaTopografica
  4.     Public Sub New()
  5.         ' The Me.InitializeComponent call is required for Windows Forms designer support.
  6.         Me.InitializeComponent()
  7.        
  8.         '
  9.         ' TODO : Add constructor code after InitializeComponents
  10.         '
  11.     End Sub
  12.     Private ObjLibretaTopografEN As LibretaTopografEN
  13.     Private ObjLibretaTopografLN As LibretaTopografLN
  14.  
  15.     [B]'AQUI ESTA EL BOTON BUSCAR[/B]
  16.  
  17.     Sub BtnbuscarClick(sender As Object, e As EventArgs)
  18.         If(txtbuscar.Text<>"")Then
  19.             If(rbsector.Checked)Then
  20.                 Try
  21.                     'rbAnno.Checked=False
  22.                     ObjLibretaTopografEN=New LibretaTopografEN
  23.                     ObjLibretaTopografLN=New LibretaTopografLN
  24.                     ObjLibretaTopografEN.sector=LTrim(RTrim(txtbuscar.Text))
  25.                     dgvlistlibretatopo.AutoGenerateColumns=False
  26.                     formato()
  27.                     dgvlistlibretatopo.DataSource= ObjLibretaTopografLN.buscarporsector(ObjLibretaTopografEN)
  28.                 Catch ex As Exception
  29.                     MsgBox(ex.Message, MsgBoxStyle.Critical,ex.Source)
  30.                 End Try
  31.             End If
  32.             If(rbAnno.Checked)Then
  33.                 Try
  34.                     rbsector.Checked=False
  35.                     ObjLibretaTopografEN=New LibretaTopografEN
  36.                     ObjLibretaTopografLN=New LibretaTopografLN
  37.                     Dim a As Integer
  38.                     a=txtbuscar.Text
  39.                     ObjLibretaTopografEN.anno=LTrim(RTrim(a))
  40.                     dgvlistlibretatopo.AutoGenerateColumns=False
  41.                     formato()
  42.                     dgvlistlibretatopo.DataSource= ObjLibretaTopografLN.buscarporanno(ObjLibretaTopografEN)
  43.                 Catch ex As Exception
  44.                     MsgBox(ex.Message, MsgBoxStyle.Critical,ex.Source)
  45.                 End Try
  46.             End If
  47.         End If
  48.     End Sub
  49.    
  50. End Class

Y EL ERROR QUE ME SALTA ES
La conversion de la cadena "2013" en el tipo date no es valida