Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2005
    Messaggi
    336

    Errore in un edit di datalist

    Qualcuno mi sa dire perchè il seguente codice per effettuare un edit in un datalist popolato da un database:
    <%@ Page Language="VB" Debug="true" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.Oledb" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Object" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">


    <script runat="server">

    ' The Cart and CartView objects temporarily store the data source
    ' for the DataGrid control while the page is being processed.
    Dim Conta As Integer
    Dim Cart As DataTable = New DataTable()
    Dim CartView As DataView
    Dim Cart1 As DataSet
    Dim Partenza As Integer
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' With a database, use an select query to retrieve the data. Because
    ' the data source in this example is an in-memory DataTable, retrieve
    ' the data from session state if it exists; otherwise create the data
    ' source.


    GetSource()

    ' The DataGrid control maintains state between posts to the server;
    ' it only needs to be bound to a data source the first time the page
    ' is loaded or when the data source is updated.


    End Sub




    Sub DataList1_EditCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)

    ' Set the EditItemIndex property to the index of the item clicked
    ' in the DataGrid control to enable editing for that item. Be sure
    ' to rebind the DateGrid to the data source to refresh the control.
    DataList1.EditItemIndex = e.Item.ItemIndex
    BindGrid()

    End Sub


    Sub DataList1_CancelCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)

    ' Set the EditItemIndex property to -1 to exit editing mode.
    ' Be sure to rebind the DateGrid to the data source to refresh
    ' the control.

    DataList1.EditItemIndex = -1
    BindGrid()
    End Sub

    Sub DataList1_UpdateCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    ' Retrieve the text boxes that contain the values to update.
    ' For bound columns, the edited value is stored in a TextBox.
    ' The TextBox is the 0th control in a cell's Controls collection.
    ' Each cell in the Cells collection of a DataGrid item represents
    ' a column in the DataGrid control.
    Dim IdCatText As System.Web.UI.WebControls.TextBox = CType(e.Item.FindControl("IdCategoria"), TextBox)
    Dim CatText As System.Web.UI.WebControls.TextBox = CType(e.Item.FindControl("Categoria"), TextBox)
    Dim DesCatText As System.Web.UI.WebControls.TextBox = CType(e.Item.FindControl("Descrizione"), TextBox)
    Dim IdCat As String = IdCatText.Text
    Dim Cat As String = CatText.Text
    Dim DesCat As String = DesCatText.Text
    Dim MyConnection As OleDbConnection
    Dim StringSelect As String
    Dim UpDateCmd As String
    Dim OleDbUpDateCmd As OleDbCommand

    MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source=" + Server.MapPath("db\NWIND.mdb"))

    MyConnection.Open()

    StringSelect = "select * from Categorie"


    UpDateCmd = "UPDATE Categorie SET NomeCategoria='" & Cat & "',Descrizione='" & DesCat & "' WHERE IdCategoria=" & IdCat

    OleDbUpDateCmd = New OleDbCommand(UpDateCmd, MyConnection)

    OleDbUpDateCmd.ExecuteNonQuery()
    OleDbUpDateCmd.Connection.Close()

    DataList1.EditItemIndex = -1
    Session("ShoppingCart") = Nothing

    GetSource()
    ' Retrieve the updated values.





    End Sub

    Sub BindGrid()

    ' Set the data source and bind to the Data Grid control.


    DataList1.DataSource = CartView

    DataList1.DataBind()

    End Sub

    Sub GetSource()

    ' For this example, the data source is a DataTable that is stored
    ' in session state. If the data source does not exist, create it;
    ' otherwise, load the data.


    Dim DS As DataSet
    Dim MyConnection As OleDbConnection
    Dim MyCommand As OleDbDataAdapter
    Dim quanti As Integer
    Dim StringSelect As String

    If Session("ShoppingCart") Is Nothing Then
    MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source=" + Server.MapPath("db\NWIND.mdb"))

    MyConnection.Open()

    StringSelect = "select * from Categorie"

    MyCommand = New OleDbDataAdapter(StringSelect, MyConnection)
    DS = New DataSet()
    MyCommand.Fill(DS, "tabella1")
    ' in quanti viene inserito il numero di record della tabella
    quanti = DS.Tables("tabella1").Rows.Count.ToString()
    Dim source As DataView = DS.Tables("tabella1").DefaultView

    DataList1.DataSource = DS

    DataList1.DataBind()
    MyConnection.Close()



    ' Store the table in session state to persist its values
    ' between posts to the server.
    Session("ShoppingCart") = DS.Tables("tabella1")


    ' Populate the DataTable with sample data.

    End If

    Cart = CType(Session("ShoppingCart"), DataTable)
    ' Create a DataView and specify the field to sort by.
    CartView = New DataView(Cart)



    End Sub







    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>

    <form id="Form2" runat="server">

    <font face="Verdana" size="-1">

    <aspataList id="DataList1" runat="server"
    BorderColor="black"
    BorderWidth="1"
    GridLines="Both"
    CellPadding="3"
    CellSpacing="0"
    Font-Names="Verdana"
    Font-Size="8pt"
    Width="150px"
    HeaderStyle-BackColor="#aaaadd"
    AlternatingItemStyle-BackColor="Gainsboro"
    EditItemStyle-BackColor="lightgreen"
    OnEditCommand="DataList1_EditCommand"
    OnUpdateCommand="DataList1_UpdateCommand"
    OnCancelCommand="DataList1_CancelCommand"
    >
    <HeaderTemplate>
    Items
    </HeaderTemplate>
    <ItemTemplate>
    <asp:LinkButton id="button1" runat="server" Text="Edit" CommandName="edit" />
    <%#DataBinder.Eval(Container.DataItem, "IdCategoria")%>
    </ItemTemplate>
    <EditItemTemplate>
    Identificativo:
    <asp:Label id="Label1" runat="server"
    Text='<%# DataBinder.Eval(Container.DataItem, "IdCategoria") %>' />


    Categoria:
    <asp:TextBox id="Text1" runat="server"
    Text='<%# DataBinder.Eval(Container.DataItem, "NomeCategoria") %>' />


    Descrizione:
    <asp:TextBox id="Text2" runat="server"
    Text='<%# DataBinder.Eval(Container.DataItem, "Descrizione") %>' />


    <asp:LinkButton id="button2" runat="server" Text="Update"
    CommandName="update" />
    <asp:LinkButton id="button3" runat="server" Text="Cancel"
    CommandName="cancel" />
    </EditItemTemplate>

    </aspataList>

    </font>
    </form>
    </form>
    </body>




    Mi restituisce il seguente errore:

    Riferimento a un oggetto non impostato su un'istanza di oggetto.
    Descrizione: Eccezione non gestita durante l'esecuzione della richiesta Web corrente. Per ulteriori informazioni sull'errore e sul suo punto di origine nel codice, vedere l'analisi dello stack.

    Dettagli eccezione: System.NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto.

    Errore nel codice sorgente:


    Riga 68: Dim CatText As System.Web.UI.WebControls.TextBox = CType(e.Item.FindControl("Categoria"), TextBox)
    Riga 69: Dim DesCatText As System.Web.UI.WebControls.TextBox = CType(e.Item.FindControl("Descrizione"), TextBox)
    Riga 70: Dim IdCat As String = IdCatText.Text
    Riga 71: Dim Cat As String = CatText.Text
    Riga 72: Dim DesCat As String = DesCatText.Text

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2005
    Messaggi
    336
    la riga segnata in rosso è la 70

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.