<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<HTML>
<script language="vb" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Not IsPostBack) Then
LoadTable()
End If
End Sub
Private Sub LoadTable()
Dim dbConn As OleDbConnection
Dim myAdapter As OleDbDataAdapter
Dim sqlCmd As String
Dim strConn As String
Dim oDataSet As DataSet = New DataSet
strConn = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("database/prodotti.mdb") + ";" & _
"Persist Security Info=False"
sqlCmd = _
" SELECT" & _
" prodotti.codice," & _
" prodotti.nome," & _
" prodotti.descrizione," & _
" prodotti.categoria as codicecategoria," & _
" categorie.categoria" & _
" FROM categorie" & _
" INNER JOIN prodotti" & _
" ON categorie.codice = prodotti.categoria"
dbConn = New OleDbConnection(strConn)
myAdapter = New OleDbDataAdapter(sqlCmd, dbConn)
myAdapter.Fill(oDataSet, "prodotti")
myDatagrid.DataSource = oDataSet.Tables("prodotti").DefaultView
myDatagrid.DataBind()
End Sub
Private Sub LoadCategorie(ByVal ddl As DropDownList, ByVal selected As String)
Dim dbConn As OleDbConnection
Dim sqlCmd As String
Dim strConn As String
strConn = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("database/prodotti.mdb") + ";" & _
"Persist Security Info=False"
sqlCmd = _
" SELECT" & _
" categorie.codice as codicecategoria," & _
" categorie.categoria" & _
" FROM categorie"
dbConn = New OleDbConnection(strConn)
dbConn.Open()
Dim dbComm As OleDbCommand = New OleDbCommand(sqlCmd, dbConn)
Dim aReader As OleDbDataReader = dbComm.ExecuteReader()
Try
ddl.Items.Clear()
While aReader.Read()
Dim li As ListItem = New ListItem( _
aReader("categoria").ToString(), _
aReader("codicecategoria").ToString() _
)
If selected = aReader("codicecategoria").ToString() Then
li.Selected = True
End If
ddl.Items.Add(li)
End While
Finally
aReader.Close()
dbConn.Close()
End Try
End Sub
Private Sub aggiornaProdotto(ByVal codice As String, ByVal nome As String, ByVal descrizione As String, ByVal categoria As String)
Dim dbConn As OleDbConnection
Dim sqlCmd As String
Dim strConn As String
strConn = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("database/prodotti.mdb") + ";" & _
"Persist Security Info=False"
sqlCmd = _
" update prodotti " & _
" set nome = '" + nome + "'," & _
" descrizione = '" + descrizione + "'," & _
" categoria = " + categoria & _
" WHERE codice = " + codice
dbConn = New OleDbConnection(strConn)
dbConn.Open()
Dim dbComm As OleDbCommand = New OleDbCommand(sqlCmd, dbConn)
Try
dbComm.ExecuteScalar()
Finally
dbConn.Close()
End Try
End Sub
Private Sub myDatagrid_OnEditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
myDatagrid.EditItemIndex = e.Item.ItemIndex
LoadTable()
End Sub
Private Sub myDatagrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
myDatagrid.EditItemIndex = -1
LoadTable()
End Sub
Private Sub myDatagrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.EditItem Then
'leggo i valori di default
Dim codiceCategoria As String = e.Item.Cells(5).Text
Dim ddl As DropDownList = Nothing
ddl = CType(e.Item.FindControl("ddlCategorie"), DropDownList)
'carico la DropdownList
LoadCategorie(ddl, codiceCategoria)
End If
End Sub
Private Sub myDatagrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Dim codice As String = e.Item.Cells(0).Text
Dim nome As String = CType(e.Item.FindControl("tbDGNome"), TextBox).Text
Dim descrizione As String = CType(e.Item.FindControl("tbDGDescrizione"), TextBox).Text
Dim categoria As String = CType(e.Item.FindControl("ddlCategorie"), DropDownList).SelectedValue
aggiornaProdotto(codice, nome, descrizione, categoria)
myDatagrid.EditItemIndex = -1
LoadTable()
End Sub
</script>
<body>
<form runat="server" ID="Form1">
<asp:datagrid id="myDatagrid" runat="server" AutoGenerateColumns="False" OnEditCommand="myDatagrid_OnEditCommand"
OnCancelCommand="myDatagrid_CancelCommand" OnItemDataBound="myDatagrid_ItemDataBound" OnUpdateCommand="myDatagrid_UpdateCommand">
<Columns>
<asp:BoundColumn DataField="codice" ReadOnly="True" HeaderText="codice"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="nome">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.nome") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=tbDGNome runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.nome") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="descrizione">
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.descrizione") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=tbDGDescrizione runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.descrizione") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="categoria">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.categoria") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlCategorie" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="codicecategoria" ReadOnly="True" HeaderText="codicecategoria"></asp:BoundColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>