begin process at 2008 09 05 22:17:02
1 237 533 membres
473 nouveaux aujourd'hui
14 313 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

INSERTION DE DONNÉES DANS UNE BASE


Information sur la source

Catégorie :ASP.Net Source .NET ( DotNet ) Niveau : Débutant Date de création : 22/05/2002 Date de mise à jour : 23/05/2002 13:00:47 Vu / téléchargé: 11 316 / 642

Note :
Aucune note

Commentaire sur cette source (3)
Ajouter un commentaire et/ou une note

Description

Ce code vous permettra d'insérer des données dans une base via un contrôle DATAGRID        

Source

  • <%@ 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>
<%@ 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>    

Conclusion

Bonne programmation !        
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

  • signaler à un administrateur
    Commentaire de magicwil le 29/07/2003 10:13:46

    Aaaaaaah, un joli petit code que voici, facilement compréhensible et qui répond à mes attentes. Merci Skyride!

  • signaler à un administrateur
    Commentaire de mage le 10/12/2003 00:57:30

    Bonjour
    tres bien ton code juste une question comment je fait quand je veut rajouter d'autre colonne exemple num_rue,tel etc..
    a chaque fois cela me marque: Erreur de mise à jour. Vérifiez les saisies

  • signaler à un administrateur
    Commentaire de hotch5 le 01/09/2006 18:47:00

    ca marche pas cette code en asp.net

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Septembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
2930     

Boutique

Boutique de goodies CodeS-SourceS