No allora non mi interessa perchè se ho uno spazio in hosting presso un provider non posso mica installare nuove dll. o no?
Sono quasi riuscito a far funzionare questo script che si trova sul libro.
Aggiorna i record, inserisce, ma non cancella.
Quando clicco viene "Errore aggiornamento database" che è la frase inserita in fondo per gestire gli errori.
lo posto, se qualche anima pia ha tempo di dare un'occhiata all'istruzione DELETE.
<%@ Page Language="VB" debug="true"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<script runat="server">
dim Conn as new OleDbConnection ("Provider=Microsoft.jet.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\test.net\data\banking.md b")
sub Page_Load(obj as Object, e as EventArgs)
if not Page.IsPostBack then
FillDataGrid()
end if
end sub
sub submit(obj as object, e as eventargs)
dim i, j as integer
dim params(7) as String
dim strText as String
dim blnGo as boolean= true
j = 0
for i = 0 to AddPanel.Controls.Count -1
if AddPanel.controls(i).GetType Is _
GetType(TextBox) then
strText = Ctype(AddPanel.Controls(i), _
TextBox).Text
if strText <> " " then
params(j) = strText
else
blnGo = False
lblMessage.Text = lblMessage.Text & _
"You forgot to enter a value for "& _
AddPanel.Controls(i).ID & "
"
lblMessage.Style("ForeColor") = "Red"
end if
j = j + 1
end if
next
if not blnGo then
exit sub
end if
dim strSQL as string = "INSERT INTO tblUsers " & _
"(FirstName, LastName, Address, City, State, Zip, Phone) VALUES (" & _
"'"¶ms(0)&"',"& _
"'"¶ms(1)&"',"& _
"'"¶ms(2)&"',"& _
"'"¶ms(3)&"',"& _
"'"¶ms(4)&"',"& _
"'"¶ms(5)&"',"& _
"'"¶ms(6)&"') "
ExecuteStatement(strSQL)
FillDataGrid()
end sub
sub dgData_Edit(obj as object, e as DataGridCommandEventArgs)
FillDataGrid(e.Item.ItemIndex)
end sub
sub dgData_Delete(obj as object, e as DataGridCommandEventArgs)
dim strSQL as string="Delete * from tblUsers" & _
"WHERE UserID=" & e.Item.ItemIndex + 1
ExecuteStatement(strSQL)
FillDataGrid()
end sub
sub dgData_Update(obj as object, e as DataGridCommandEventArgs)
if UpdateDataStore(e) then
FillDataGrid(-1)
end if
end sub
sub dgData_Cancel(obj as object, e as DataGridCommandEventArgs)
FillDataGrid(-1)
end sub
sub dgData_PageIndexChanged(obj as Object, e as DataGridPageChangedEventArgs)
dgData.DataBind()
end sub
function UpdateDataStore(e as DataGridCommandEventArgs) as boolean
dim i,j as integer
dim params(7) as string
dim strText as string
dim blnGo as boolean = True
j=0
for i=1 to e.Item.Cells.Count -3
strText = Ctype(e.Item.Cells(i).Controls(0),TextBox).Text
if strText <> "" then
params(j) = strText
j=j+1
else
blnGo=false
lblMessage.Text=lblMessage.Text &"You forgot to enter a value
"
end if
next
if not blnGo then
return false
exit function
end if
dim strSQL as string = "UPDATE tblUsers SET " & _
"FirstName = '"¶ms(0)&"',"& _
"LastName = '"¶ms(1)&"',"& _
"Address = '"¶ms(2)&"',"& _
"City = '"¶ms(3)&"',"& _
"State = '"¶ms(4)&"',"& _
"Zip = '"¶ms(5)&"',"& _
"Phone = '"¶ms(6)&"'"& _
"WHERE UserID="&Ctype(e.Item.Cells(0).Controls(1), Label).Text
ExecuteStatement(strSQL)
return blnGO
end function
sub FillDataGrid(Optional EditIndex as Integer = -1)
dim objCmd as new OleDbCommand _
("select * from tblUsers order by LastName ASC", Conn)
dim objReader as OleDbDataReader
try
objCmd.Connection.Open()
objReader = objCmd.ExecuteReader()
catch ex as Exception
lblMessage.Text="Error retrieving from the database"
end try
dgData.DataSource = objReader
if not EditIndex.Equals(Nothing) then
dgData.EditItemIndex = EditIndex
end if
dgData.DataBind()
objReader.Close()
objCmd.Connection.Close()
end sub
function ExecuteStatement(strSQL)
dim objCmd as new OleDbCommand(strSQL, Conn)
try
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
catch ex as Exception
lblMessage.Text="Errore di aggiornamento del database"
end try
objCmd.Connection.Close()
end function
</script>
<html>
<body>
<asp:label ID="lblMessage" runat="server"/>
<form runat="server">
<asp:datagrid id="dgData" runat="server" BorderColor="black" GridLines="Vertical" CellPadding="4" CellSpacing="0" Width="550" Font-Names="Arial" Font-Size="8pt" ShowFooter="true" HeaderStyle-BackColor="#CCCC99" FooterStyle-BackColor="#cccc99" ItemStyle-BackColor="#ffffff" AlternatingItemStyle-BackColor="#cccccc" AutoGenerateColumns="false" OnDeleteCommand="dgData_Delete" OnEditCommand="dgData_Edit" OnCancelCommand="dgData_Cancel" OnUpdateCommand="dgData_Update" OnPageIndexChanged="dgData_pageIndexChanged">
<columns>
<asp:templatecolumn HeaderText="id">
<itemtemplate>
<asp:label ID="name" runat="server" Text='<%# Container.DataItem("UserID")%>'/>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn HeaderText="FirstName"
DataField="FirstName"/>
<asp:boundcolumn HeaderText="LastName"
DataField="LastName"/>
<asp:boundcolumn HeaderText="Address"
DataField="Address"/>
<asp:boundcolumn HeaderText="City"
DataField="City"/>
<asp:boundcolumn HeaderText="State"
DataField="State"/>
<asp:boundcolumn HeaderText="Zip"
DataField="Zip"/>
<asp:boundcolumn HeaderText="Phone"
DataField="Phone"/>
<asp:editcommandcolumn EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit"/>
<asp:buttoncolumn HeaderText="" Text="Delete" CommandName="delete"/>
</columns>
</asp:datagrid>
<aspanel ID="AddPanel" runat="server">
<table>
<tr>
<td width="100" valign="top">
Nome e cognome:
</td>
<td width="300" valign="top">
<asp:textbox ID="tbFName" runat="server"/>
<asp:textbox ID="tbLName" runat="server"/>
</td>
</tr>
<tr>
<td valign="top">Indirizzo:</td>
<td valign="top">
<asp:textbox ID="tbAddress" runat="server"/>
</td>
</tr>
<tr>
<td valign="top">Città, stato, zip:</td>
<td valign="top">
<asp:textbox ID="tbCity" runat="server"/>
<asp:textbox ID="tbState" runat="server" size=2/>
<asp:textbox ID="tbZip" runat="server" size=5/>
</td>
</tr>
<tr>
<td valign="top">Telefono:</td>
<td valign="top">
<asp:textbox ID="tbPhone" runat="server" size=11/>
</p>
</td>
</tr>
<tr>
<td colspan="2" valign="top" align="right">
<asp:button ID="btSubmit" runat="server" Text="Add" OnClick="Submit"/>
</td>
</tr>
</table>
</aspanel>
</form>
</body>
</html>