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

    Problemi con codice per imparare ad utilizzare ADO.NET

    Allora sotto riporto il codice che ho utilizzato per imparare ad utilizzare ADO.NET.
    Creo una connessione riempio un dataset e poi cerco di modificarlo (stampando le modifiche)
    fino alla seconda stampata tutto ok alla terza stampata però mi viene fuori un errore con un'indice dell'array oltrepassato ma non ho capito per quale motivo
    lo testate?



    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>
    <script runat="server">

    sub Page_Load (obj as Object, e as EventArgs)
    dim Con as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Inetpub\wwwroot\esempi\prova.mdb")
    Dim ds AS new DataSet("MyDataSet")
    Dim objCmd as new OleDbDataAdapter ("SELECT * FROM tblUsers WHERE id < 10", Con)

    objCmd.Fill(ds,"tblUsers")

    'Prima Stampata
    Dim dTable as DataTable = ds.Tables("tblUsers")
    Dim CurrRows() as DataRow = dTable.Select(Nothing, Nothing, DataViewRowState.CurrentRows)
    Dim I, J as integer
    Dim strOutput as String
    FOR I = 0 to CurrRows.Length - 1
    FOR J = 0 TO dTable.Columns.Count - 1
    strOutput = strOutput & dTable.Columns(J).ColumnName & " = " & CurrRows(I)(J).ToString & " "
    NEXT
    strOutput = strOutput & "<hr>"
    NEXT

    Response.Write(strOutput)
    Response.Write("


    ")
    ds.Tables("tblUsers").Rows(2)(3) = "Faccia di Merda"

    'Seconda Stampata
    Dim dTable2 as DataTable = ds.Tables("tblUsers")
    Dim CurrRows2() as DataRow = dTable2.Select(Nothing, Nothing, DataViewRowState.CurrentRows)
    Dim I2, J2 as integer
    Dim strOutput2 as String
    FOR I2 = 0 to CurrRows2.Length - 1
    FOR J2 = 0 TO dTable2.Columns.Count - 1
    strOutput2 = strOutput2 & dTable2.Columns(J2).ColumnName & " = " & CurrRows(I2)(J2).ToString & " "
    NEXT
    strOutput2 = strOutput2 & "<hr>"
    NEXT
    Response.Write(strOutput2)

    dim dr as DataRow = ds.Tables("tblUsers").NewRow()
    dr(0) = "100"
    dr(1) = "Zampetti"
    dr(2) = "Via della Cacca 2902"
    dr(3) = "Sant'Odorico"
    dr(4) = "UD"
    dr(5) = "1981"
    dr(6) = "320392938329393"
    dr(7) = "339-5031146"
    ds.Tables("tblUsers").Rows.Add(dr)

    'Terza Stampata
    Dim dTable3 as DataTable = ds.Tables("tblUsers")
    Dim CurrRows3() as DataRow = dTable3.Select(Nothing, Nothing, DataViewRowState.CurrentRows)
    Dim I3, J3 as integer
    Dim strOutput3 as String
    FOR I3 = 0 to CurrRows3.Length - 1
    FOR J3 = 0 TO dTable3.Columns.Count - 1
    strOutput3 = strOutput3 & dTable3.Columns(J3).ColumnName & " = " & CurrRows(I3)(J3).ToString & " "
    NEXT
    strOutput3 = strOutput3 & "<hr>"
    NEXT
    Response.Write(strOutput3)


    objCmd.UpdateCommand = new OleDbCommand
    objCmd.UpdateCommand.cOmmandText = "Update tblUsers SET city='ASPVille' WHERE id = 3"
    objCmd.UpdateCommand.Connection = Con

    objCmd.InsertCommand = new OleDbCommand
    objCmd.InsertCommand.CommandText = "INSERT INTO tblUsers(nome, cognome,indirizzo) VALUES ('Lurido', 'Lurido', 'Stronzo')"
    objCmd.InsertCommand.Connection = Con
    end sub

    function Stampa(dtSt as DataSet)

    end function

    </script>

  2. #2
    ah ragazzi allora il db si chiama prova.mdb
    la tabella tblUsers

    e i campi sono
    id Contatore
    nome Testo
    cognome Testo
    indirizzo Testo
    citta Testo
    stato Testo
    zip Testo
    telefono Testo

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    236

    il problema è

    che prima della terza stampa, tu inserisci una riga con dei dati nel dataset, ma tu li hai inseriti solamente nel datataset senza confermare l'inserimento, quindi quando si trova a fare il ciclo trova una riga in + rispetto a quelle reale e quando la raggiunge ti da errore perchè per lui quella che tu hai inserito è ancora in sospeso....
    spero di essere stato chiaro

    Koning

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    236

    scusami....

    ho detto una cavolata....l'errore è che quando scrivi la stringa nella terza stampa fai rifermineto alla variabile CurrRows e non CurrRows3

    ciao

    koning

  5. #5
    scusa ma dove?
    non ho capito puoi indicarmi la riga?

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    236
    strOutput3 = strOutput3 & dTable3.Columns(J3).ColumnName & " = " & CurrRows(I3)(J3).ToString & " "

    qui

    strOutput3 = strOutput3 & dTable3.Columns(J3).ColumnName & " = " & CurrRows3(I3)(J3).ToString & " "

  7. #7
    no non ha nulla a che fare perchè con la seconda stampa dove uso currRows al posto di CurrRows2 funziona tutto

  8. #8
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    236

    si ok...

    ma dopo la seconda stampa inserisci un record e quindi sballi la numerazione....io ho provato il tuo codice e mettendo la correzione che ti ho indicato funziona

    koning

  9. #9
    ok ora ci riprovo!!
    sei molto molto gentile

  10. #10
    ottimo funziona !!!!
    ma vai sei un grande!

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.