<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
' On définit la connection
Dim Conn As New OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Server.MapPath("\testnet\test.mdb"))
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)
' Insertion d'un enregistrement
dim i, j as integer
dim tabInsert(2) as string
dim strText as string
dim TraitError as boolean = true
j = 0
for i = 0 to PanelInsert.Controls.Count - 1
if PanelInsert.controls(i).GetType Is GetType(TextBox) then
strText = Ctype(PanelInsert.Controls(i), TextBox).Text
if strText <> "" then
tabInsert(j) = strText
else
TraitError = false
lblMessage.Text = lblMessage.Text & "Entrer une valeur pour " & PanelInsert.Controls(i).ID & "<br>"
end if
j = j + 1
end if
next
if not TraitError then
exit sub
end if
dim strSQL as string = "INSERT INTO CLIENT (CL_NOM, CL_PRENOM ) VALUES (" & _
"'" & tabInsert(0) & "'" & "," & "'" & tabInsert(1) & "')"
'lblMessage.Text = strSQL
ExecuteSQL(strSQL)
FillDataGrid()
end sub
sub FillDataGrid()
'Ouverture de la connexion
dim objCmd as new OleDbCommand _
("select * from CLIENT", Conn)
dim objReader as OleDbDataReader
try
objCmd.Connection.Open()
objReader = objCmd.ExecuteReader()
catch ex as Exception
lblMessage.Text = "Erreur de liaison avec la base de données."
end try
dgData.DataSource = objReader
dgData.DataBind()
objReader.Close
objCmd.Connection.Close()
end sub
function ExecuteSQL(strSQL)
dim objCmd as new OleDbCommand(strSQL, Conn)
try
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
catch ex as Exception
lblMessage.Text = "Erreur de mise à jour." & _
" Vérifiez les saisies."
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="450"
Font-Names="Arial"
Font-Size="8pt"
ShowFooter="True"
HeaderStyle-BackColor="#CCCCCC"
FooterStyle-BackColor="#CCCCCC"
ItemStyle-BackColor="#ffffff"
AlternatingItemStyle-Backcolor="#cccccc"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label id="lblId" runat="server" Text='<%# Container.DataItem("CL_ID") %>'/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="NOM" DataField="CL_NOM" />
<asp:BoundColumn HeaderText="PRENOM" DataField="CL_PRENOM" />
</Columns>
</asp:DataGrid>
<asp:Panel id="PanelInsert" runat="server">
<table>
<tr>
<td width="100" valign="top">Nom :</td>
<td width="300" valign="top">
<asp:TextBox id="CL_NOM" runat="server"/>
</td>
</tr>
<tr>
<td width="100" valign="top">Prénom :</td>
<td width="300" valign="top">
<asp:TextBox id="CL_PRENOM" runat="server"/>
</td>
</tr>
<tr>
<td colspan="2" valign="top" align="right">
<asp:Button id="btSubmit" runat="server" text="Envoyer" OnClick="Submit" />
</td>
</tr>
</table>
</asp:Panel>
</form>
</body>
</html>