Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2001
    Messaggi
    573

    Problemi con funzione che formatta data

    ho sempre usato questa funzione per formattare una data ad esempio in un datagrid:
    Function FormattaData(ByVal Valore As Date) As String
    If Microsoft.VisualBasic.IsDate(Valore) Then
    Return System.DateTime.Parse(Valore).ToString("dd/MM/yyyy")
    Else
    Return ""
    End If
    End Function

    e poi...
    <%#FormattaData(Container.DataItem("MiaData"))%>

    Questo con db access. Ora con sql mi da errore:
    Cast from type 'DBNull' to type 'Date' is not valid.

    Perchè? :master:

    Ciao Elisa

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127
    prova con If Not valore Is Nothing ..

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2001
    Messaggi
    573
    già provato... sempre stesso errore....

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127
    If Not IsDBNull(Valore) And Microsoft.VisualBasic.IsDate(Valore) Then
    Return System.DateTime.Parse(Valore).ToString("dd/MM/yyyy")
    Else
    Return ""
    End If
    End Function

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2001
    Messaggi
    573
    Niente stesso errore.... non gli piace proprio trovare il campo vuoto eppure lo dovrebbe agirare il problema

    Non so se hai altre idee... ma se provo con <%# DataBinder.Eval(Container.DataItem, "nomecolonna")%>> solo non so quale parametro specificare (e non lo trovo) per avere una formattazione dd/MM/yyyy

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2001
    Messaggi
    573
    ho provato con una cosa del genere:
    <%#DataBinder.Eval(Container.DataItem,"DataStipula ", "{0:dd/MM/yy}")%>

    oppure
    <%#DataBinder.Eval(Container.DataItem,"DataStipula ", "{0:d}")%>

    non da errore ma nulla si formatta! incredibile...
    Certo la funzione mi faceva comodo ...ma che ha sql con le date e i campi del db vuoti?!?!?!?!

  7. #7
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Originariamente inviato da elisa_rm74
    ho provato con una cosa del genere:
    <%#DataBinder.Eval(Container.DataItem,"DataStipula ", "{0:dd/MM/yy}")%>

    oppure
    <%#DataBinder.Eval(Container.DataItem,"DataStipula ", "{0:d}")%>

    non da errore ma nulla si formatta! incredibile...
    Certo la funzione mi faceva comodo ...ma che ha sql con le date e i campi del db vuoti?!?!?!?!
    prova se ti funziona
    codice:
        '--------------------------------------------------------------------------------
        'Converte DBNull nella stringa nulla ""
        'Esempio: PrintLn(Me.NullToString("15/3/1955", "d MMMM yyyy", GetType(DateTime)))
        '--------------------------------------------------------------------------------
        Public Overloads Function NullToString$(ByVal v As Object, ByVal Formato$, ByVal Tipo As Type)
            If IsDBNull(v) Then
                Return ""
            Else
                If Tipo Is GetType(DateTime) Then
                    If IsDate(v) Then
                        Return CDate(v).ToString(Formato)
                    Else
                        Return CType(v, String)
                    End If
    
                ElseIf Tipo Is GetType(Double) Then
                    If IsNumeric(v) Then
                        Return CDbl(v).ToString(Formato)
                    Else
                        Return CType(v, String)
                    End If
    
                ElseIf Tipo Is GetType(Integer) Then
                    If IsNumeric(v) Then
                        Return CInt(v).ToString(Formato)
                    Else
                        Return CType(v, String)
                    End If
    
                ElseIf Tipo Is GetType(Long) Then
                    If IsNumeric(v) Then
                        Return CLng(v).ToString(Formato)
                    Else
                        Return CType(v, String)
                    End If
    
                ElseIf Tipo Is GetType(Decimal) Then
                    If IsNumeric(v) Then
                        Return CDec(v).ToString(Formato)
                    Else
                        Return CType(v, String)
                    End If
    
                ElseIf Tipo Is GetType(String) Then
                    Return CType(v, String)
                End If
    
                Return CType(v, String)
            End If
    
        End Function
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.