- <%@ 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 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 CLIENT WHERE CL_ID = " & Ctype(e.Item.Cells(0).Controls(1), Label).Text
-
- 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
-
- function UpdateDataStore(e as DataGridCommandEventArgs) as boolean
-
- dim i,j as integer
- dim tabUpdate(1) as string
- dim strText as string
- dim blnGo as boolean = true
-
- j = 0
-
- ' -3 pour ne pas compter les colonnes EDITION et SUPPRESSION
- for i = 1 to e.Item.Cells.Count - 3
- strText = Ctype(e.Item.Cells(i).Controls(0), TextBox).Text
-
- ' Combien de colonnes : e.Item.Cells.Count
- ' 9 cellules : ID, nom, etc...suppression
-
-
- if strText <> "" then
- tabUpdate(j) = strText
- j = j + 1
- else
- blnGo = false
- lblMessage.Text = "Veuillez renseigner tous les champs svp"
- end if
- next
-
- if not blnGo then
- return false
- exit function
- end if
-
- dim strSQL as string = "UPDATE CLIENT SET " & _
- "CL_NOM = '" & tabUpdate(0) & "'," & _
- "CL_VILLE = '" & tabUpdate(1) & "'" & _
- " WHERE CL_ID = " & Ctype(e.Item.Cells(0).Controls(1), Label).text
-
- ExecuteStatement(strSQL)
- return blnGo
- end function
-
- sub FillDataGrid(Optional EditIndex as integer=-1)
- '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 = "Liaison avec la base de données erronée"
- 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 = "Erreur lors de la mise à jour des données"
- 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"
- OnDeleteCommand="dgData_Delete"
- OnEditCommand="dgData_Edit"
- OnCancelCommand="dgData_Cancel"
- OnUpdateCommand="dgData_Update">
-
- <Columns>
- <asp:TemplateColumn HeaderText="ID">
- <ItemTemplate>
- <asp:Label id="Id" runat="server" Text='<%# Container.DataItem("CL_ID") %>'/>
- </ItemTemplate>
- </asp:TemplateColumn>
-
- <asp:BoundColumn HeaderText="Nom" DataField="CL_NOM" />
- <asp:BoundColumn HeaderText="Ville" DataField="CL_VILLE"/>
-
- <asp:EditCommandColumn
- EditText="Edition"
- CancelText="Annuler"
- UpdateText="Mise à jour"
- HeaderText="Edition"/>
-
- <asp:ButtonColumn HeaderText="Suppression" text="Supprimer" CommandName="delete" />
- </Columns>
- </asp:DataGrid>
- </form>
- </body>
- </html>
<%@ 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 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 CLIENT WHERE CL_ID = " & Ctype(e.Item.Cells(0).Controls(1), Label).Text
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
function UpdateDataStore(e as DataGridCommandEventArgs) as boolean
dim i,j as integer
dim tabUpdate(1) as string
dim strText as string
dim blnGo as boolean = true
j = 0
' -3 pour ne pas compter les colonnes EDITION et SUPPRESSION
for i = 1 to e.Item.Cells.Count - 3
strText = Ctype(e.Item.Cells(i).Controls(0), TextBox).Text
' Combien de colonnes : e.Item.Cells.Count
' 9 cellules : ID, nom, etc...suppression
if strText <> "" then
tabUpdate(j) = strText
j = j + 1
else
blnGo = false
lblMessage.Text = "Veuillez renseigner tous les champs svp"
end if
next
if not blnGo then
return false
exit function
end if
dim strSQL as string = "UPDATE CLIENT SET " & _
"CL_NOM = '" & tabUpdate(0) & "'," & _
"CL_VILLE = '" & tabUpdate(1) & "'" & _
" WHERE CL_ID = " & Ctype(e.Item.Cells(0).Controls(1), Label).text
ExecuteStatement(strSQL)
return blnGo
end function
sub FillDataGrid(Optional EditIndex as integer=-1)
'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 = "Liaison avec la base de données erronée"
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 = "Erreur lors de la mise à jour des données"
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"
OnDeleteCommand="dgData_Delete"
OnEditCommand="dgData_Edit"
OnCancelCommand="dgData_Cancel"
OnUpdateCommand="dgData_Update">
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label id="Id" runat="server" Text='<%# Container.DataItem("CL_ID") %>'/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Nom" DataField="CL_NOM" />
<asp:BoundColumn HeaderText="Ville" DataField="CL_VILLE"/>
<asp:EditCommandColumn
EditText="Edition"
CancelText="Annuler"
UpdateText="Mise à jour"
HeaderText="Edition"/>
<asp:ButtonColumn HeaderText="Suppression" text="Supprimer" CommandName="delete" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>