Ciao a tutti!
Da qualche settimana mi sono messo in testa di iniziare a programmare in ASP.net...
Stavo provando a realizzare uno script per la gestione delle news, ma mi sono bloccato nella realizzazione della pagina "modifica_news.aspx", che dovrebbe modificare le news. Ecco il suo codice:
codice:
<%@ Page Language="VB" validaterequest="false" %>
<%@ assembly name="ADODB" %>
<%@ import Namespace="ADODB" %>
<%@ import Namespace="System.Data" %>
<script runat="server">
' Creo la connessione ed il recordset
Private Connect As New ADODB.Connection
Private Connectb As New ADODB.Connection
Private recset As New ADODB.Recordset
' Page_Load, l'evento che si verificher al caricamento della pagina
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Verifica se si e' amministratori
IF Session("amministratore") = False Then
Response.Redirect("default.aspx")
End IF
' Dichiara la variabile "ID" che conterrà l'ID della news passata
dim ID = Request("ID")
' Se come ID non è stato passato un valore valido
' Imposta l'ID ad 1
IF NOT ISNumeric(ID) or Len(ID) = 0 Then
ID = 1
End IF
' Se l'ID e' uguale a 0
' Imposta l'ID a 1
IF ID = 0 Then
ID = 1
End IF
ID = Int(ID)
' Apro la connessione ed il recordset
Connect.Open("driver={Microsoft Access Driver (*.mdb)};dbq=" &
Server.MapPath("/mdb-database/database.mdb"))
' Eseguo la query al database per estrarre la news
RecSet.Open("SELECT TOP 1 * FROM news Where ID = " & ID &"", Connect)
' Verifica se la news realmente esiste
If RecSet.EOF Then
' La news non esiste (porta l'amministratore nella pagina "Gestione_news.asp")
Response.Redirect ("Gestione_news.aspx")
Else
' La news esiste: imposta nei vari campi del modulo i vari valori
Autore.Text = RecSet("Autore").Value
Contenuto.Text = Replace(RecSet("Contenuto").Value, "
", chr(13))
Titolo.Text = RecSet("Titolo").Value
End If
RecSet.Close
Connect.Close
End Sub
' Evento che si verifica al click sul tasto "Modifica News"
' (modifica la news nel db)
Sub Modifica_news_Click(sender As Object, e As EventArgs)
' Verifica che tutti i campi del modulo siano stati compilati
IF Page.Isvalid Then
' Apro la connessione
Connectb.Open("driver={Microsoft Access Driver (*.mdb)};dbq=" &
Server.MapPath("/mdb-database/database.mdb"))
' Eseguo la query per modifica la news nel database
Connectb.Execute("Update news set Titolo = '" & Titolo.Text &"', Contenuto = '" &
Replace(Contenuto.Text, chr(13), "
") &"', Autore = '" & Autore.Text &"' where id = 21", 2)
Connectb.Close
' Porta l'amministratore in "Gestione_news.aspx"
Response.Redirect("Gestione_news.aspx")
End IF
End Sub
</script>
<html>
<head>
<title>Gestione news - Aggiungi news - Amministrazione</title>
</head>
<body>
<form runat="server">
<p align="center">
<font face="Verdana" size="4">Gestione News -Modifica
news</font>
</p>
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
<tr>
<td width="19%">
<font face="Verdana" size="2">Titolo news:</font></td>
<td width="81%">
<font face="Verdana" size="2">
<asp:TextBox id="Titolo" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="Validatore1" runat="server"
ControlToValidate="Titolo" ErrorMessage="*"></asp:RequiredFieldValidator>
</font></td>
</tr>
<tr>
<td width="19%">
<font face="Verdana" size="2">Contenuto news:</font></td>
<td width="81%">
<asp:TextBox id="Contenuto" runat="server" TextMode="MultiLine"
Height="133px" Width="372px"></asp:TextBox>
<asp:RequiredFieldValidator id="Validatore2" runat="server"
ControlToValidate="Contenuto" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="19%">
<font face="Verdana" size="2">Autore news:</font></td>
<td width="81%">
<font face="Verdana" size="2">
<asp:TextBox id="Autore" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="Validatore3" runat="server"
ControlToValidate="Autore" ErrorMessage="*"></asp:RequiredFieldValidator>
</font></td>
</tr>
<tr>
<td width="100%" colspan="2">
<font face="Verdana" size="2">
<asp:Button id="Modifica_news" onclick="Modifica_news_Click"
runat="server" Text="Modifica news"></asp:Button>
</font></td>
</tr>
</tbody>
</table>
<a href="Gestione_news.aspx"><font face="Verdana" size="2">Torna alla Gestione
delle news</font></a>
</p>
</p>
</form>
</body>
</html>
Alla pagina viene passato l'id della news (modifica_news.aspx?id=ID NEWS) e sembra tutto funzionare. Però quando tento di modificare la news nel db, non la modifica! Mi dite dove ho sbagliato?
Salvatore