Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Cast from type 'DBNull' to type 'Currency' is not valid.

    ciao,
    questa query funzionante su ACCESS:
    codice:
    sql = "SELECT Selezioni.Nome, Selezioni.ID, Selezioni.Img1, Avg(Votazioni.Voto) AS Media, " 
    sql = sql & "Count(Votazioni.Voto) AS TotaleVoti "
    sql = sql & "FROM Selezioni LEFT JOIN Votazioni ON Selezioni.ID = Votazioni.Partecipante "
    sql = sql & "WHERE SELEZIONI.Sesso = 'Donna' " 
    sql = sql & "GROUP BY Selezioni.Nome, Selezioni.ID, Selezioni.Img1 "
    sql = sql & "ORDER BY AVG(Votazioni.Voto) DESC"
    quando la eseguo mi da questo errore:

    Cast from type 'DBNull' to type 'Currency' is not valid.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'Currency' is not valid.

    Source Error:


    Line 70: query.Fill(querydataset, startRecord, Int32.Parse(PageSize), "ext_content")
    Line 71: queryres.DataSource = querydataset
    Line 72: queryres.DataBind() -> errore


    come mai? cosa può essere?

  2. #2
    l'errore è dovuto a questa linea:

    <%# FormatNumber(DataBinder.Eval(Container.DataItem,"M edia"), 2, , ,TriState.UseDefault) %>

    praticamente se Media è vuoto ricevo errore.


    per lasciare il FormatNumber come potrei fare? come devo modificare il codice?

  3. #3
    ho pensato di richiamare una funzione passandogli il valore del db

    però non ne riesco ad uscire
    codice:
    Public Function Controlla(ByVal valore)
            If valore Then
                return FormatNumber(valore, 2, , , TriState.UseDefault)
            End If
    End Function
    perchè quando arriva il valore DBNULL non riesco a gestirlo.


  4. #4
    ho risolto in questo modo:
    codice:
    If Not valore.Equals(System.DBNull.Value) Then
          Return FormatNumber(valore, 2, , , TriState.UseDefault)
    Else
         Return "0"
    End If
    
    o volendo anche in quest'altro
    
    Try
      Return valore
    Catch ex As InvalidCastException
      'cosa succede se la tabella è ancora vuota?
      'per esempio possiamo assegnare 0 alla nostra variabile!
      Return 0
    Catch ex As Exception
      caso in cui vada storto qualcosa...
      Response.Write(ex.Message & "Errore imprevisto! il programmatore è una sega :gren: ")
    End Try

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    1,482
    If Not IsDBNull(valore) Then


    C0sì è più bello vero?

    Hey hey, my my Rock and roll can never die!

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 © 2025 vBulletin Solutions, Inc. All rights reserved.