Ciao
sto provando ad utilizzare la funzione EDIT del datagrid.
ho seguito attentamente il libro... ma ho un errore strano che non so risolvere.
questo è il codice che ho fra i tag <script>
codice:
Sub dgr_uppa(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim strsql As String
Dim cmdsql As MySqlCommand
Dim intid_utente As Integer
Dim txtusername As TextBox
Dim txtpassword As TextBox
Dim txtstatus As TextBox
Dim txtid_database As TextBox
Dim txtnotes As TextBox
Dim strusername As String
Dim strpassword As String
Dim strstatus As String
Dim intid_database As Integer
Dim strnotes As String
intid_utente = dgr.DataKeys(e.Item.ItemIndex)
txtusername = e.Item.Cells(1).Controls(0)
txtpassword = e.Item.Cells(2).Controls(0)
txtstatus = e.Item.Cells(3).Controls(0)
txtid_database = e.Item.Cells(4).Controls(0)
txtnotes = e.Item.Cells(5).Controls(0)
strusername = txtusername.Text
strpassword = txtpassword.Text
strstatus = txtstatus.Text
intid_database = txtid_database.Text
strnotes = txtnotes.Text
strsql = "UPDATE utenti set username=@username, pass=@password, status=@status, id_database=@id_database, notes=@notes WHERE id_utente=@id_utente"
cmdsql = New MySqlCommand(strsql, myConn)
cmdsql.Parameters.Add("@username", strusername)
cmdsql.Parameters.Add("@password", strpassword)
cmdsql.Parameters.Add("@status", strstatus)
cmdsql.Parameters.Add("@id_database", intid_database)
cmdsql.Parameters.Add("@notes", strnotes)
cmdsql.Parameters.Add("@id_utente", intid_utente)
Response.Write(strsql)
myConn.Open()
cmdsql.ExecuteNonQuery()
myConn.Close()
dgr.EditItemIndex = -1
binddgr()
End Sub
mentre questo è quello che ho nella parte html
codice:
<asp:DataGrid
OnEditCommand="dgr_edit"
OnUpdateCommand="dgr_uppa"
OnCancelCommand="del_dgr"
ID="dgr"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:BoundColumn HeaderText="ID utente" DataField="id_utente" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Utente" DataField="username"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Password" DataField="pass"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Stato" DataField="status"></asp:BoundColumn>
<asp:BoundColumn HeaderText="ID Database" DataField="id_database"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Note" DataField="notes"></asp:BoundColumn>
<asp:EditCommandColumn HeaderText="AZIONI" EditText="Modifica" UpdateText="Aggiorna" CancelText="Cancella"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
l'errore che ricevo è questo.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
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.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Source Error:
Line 57: Dim strnotes As String
Line 58:
Line 59: intid_utente = dgr.DataKeys(e.Item.ItemIndex)
Line 60: txtusername = e.Item.Cells(1).Controls(0)
Line 61: txtpassword = e.Item.Cells(2).Controls(0)