Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di donaz
    Registrato dal
    Jan 2002
    Messaggi
    206

    Gestire il caso del datareader vuoto

    Salve a tutti
    Scusate la domanda stupida però in questo momento (forse di confusione) non riesco a capire come poter visualizzare un messaggio del tipo "Per la selezione fatta non risulta nessun dato" quando il contenuto di un datareader è vuoto.

    Ho una banale pagina con un droplist che in funzione della scelta interroga una tabella per poi visualizzarli. Questo è il pezzo di codice dove vengono estratti i dati in funzione del parametro scelto nel droplist. Come faccio a testare il caso in cui non ho dati?

    'sub per estrarre i dati in funzione della scelta fatta dal droplist

    Sub bntRisco_Click(sender as Object, e as EventArgs)
    Dim aCookie As HttpCookie = Request.Cookies("permessi")

    Dim Label1 as String
    Label1 = Server.HtmlEncode(aCookie.Value)

    If Label1 = "de" Then
    Dim recupero_codente as String = Request.QueryString("codente")
    Dim recupero_denom as String = Request.QueryString("denominazione")

    Dim strConn as String ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/civitas/mdb-database/civitas.mdb")
    Dim objConn as New OleDbConnection(strConn)
    objConn.Open()

    Dim strSQL as String = "SELECT tesnumopercon, desente, tesannruo,tescodfisc, dateven, (tesimprisc/100) AS importo FROM testata_incassi INNER JOIN tabenti ON testata_incassi.tesente=tabenti.codente WHERE testata_incassi.tesente=" & "'" & recupero_codente & "'" & "ORDER BY " & "'" & lstRisco.SelectedItem.Value & "'"

    Dim objCommand as New OleDbCommand(strSQL, objConn)
    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
    dgrisco.DataSource = objDataReader
    dgrisco.DataBind()

    lblMessaggio.Text = "Incassi Per " & lstRisco.SelectedItem.Text & " per il comune " & recupero_denom
    end if
    End Sub

    Come posso testare dgrisco?

    Grazie a tutti
    Cordialità Donaz

  2. #2
    Utente di HTML.it L'avatar di donaz
    Registrato dal
    Jan 2002
    Messaggi
    206
    Ho provato a fare questo tipo di controllo però è come se la condizione fosse sempre vera.

    'sub per estrarre i dati in funzione della scelta fatta dal droplist

    Sub bntRisco_Click(sender as Object, e as EventArgs)


    Dim aCookie As HttpCookie = Request.Cookies("permessi")

    Dim Label1 as String

    Label1 = Server.HtmlEncode(aCookie.Value)

    If Label1 = "de" Then
    Dim recupero_codente as String = Request.QueryString("codente")
    Dim recupero_denom as String = Request.QueryString("denominazione")


    Dim strConn as String ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/civitas/mdb-database/civitas.mdb")
    Dim objConn as New OleDbConnection(strConn)
    objConn.Open()



    Dim strSQL as String = "SELECT tesnumopercon, desente, tesannruo,tescodfisc, dateven, (tesimprisc/100) AS importo FROM testata_incassi INNER JOIN tabenti ON testata_incassi.tesente=tabenti.codente WHERE testata_incassi.tesente=" & "'" & recupero_codente & "'" & "ORDER BY " & "'" & lstRisco.SelectedItem.Value & "'"

    Dim objCommand as New OleDbCommand(strSQL, objConn)

    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader

    If ObjDataReader.hasRows then


    lblMessaggio.text = "Non risultano incassi per " & lstRisco.SelectedItem.Text & " per il comune " & recupero_denom

    else
    dgrisco.DataSource = objDataReader
    dgrisco.DataBind()

    lblMessaggio.Text = "Incassi Per " & lstRisco.SelectedItem.Text & " per il comune " & recupero_denom

    end if

    ObjDataReader.Close()

    end if
    End Sub
    Cordialità Donaz

  3. #3
    Utente di HTML.it L'avatar di donaz
    Registrato dal
    Jan 2002
    Messaggi
    206
    Ho risolto ho modificato il controllo con la funzione hasRows sul datareader in questo modo:

    If not ObjDataReader.hasRows then

    lblMessaggio.text = "Non risultano incassi per " & lstRisco.SelectedItem.Text & " per il comune " & recupero_denom

    else

    dgrisco.DataSource = objDataReader
    dgrisco.DataBind()

    lblMessaggio.Text = "Incassi Per " & lstRisco.SelectedItem.Text & " per il comune " & recupero_denom

    end if
    Cordialità Donaz

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.