begin process at 2010 03 22 12:33:35
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

ASP.Net

 > INSERTION DE DONNÉES DANS UNE BASE

INSERTION DE DONNÉES DANS UNE BASE


 Information sur la source

Note :
Aucune note
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é :12 257 / 689

Auteur : Skyride

Ecrire un message privé
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 !        

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

COMPOSANT ASPMAIL PILOTÉ VIA SQL SERVER
Source .NET (Dotnet) LISTER LES DOSSIERS D'UN RÉPERTOIRE
Source .NET (Dotnet) LISTER LES FICHIERS D'UN RÉPERTOIRE
Source .NET (Dotnet) INFORMATIONS D'UN FICHIER
Source avec Zip Source .NET (Dotnet) GÉNÉRER UN DOCUMENT HTML À PARTIR DE DONNÉES CONTENUES DANS ...

 Sources de la même categorie

Source avec Zip Source .NET (Dotnet) UTILISATION DE LA MÉTHODE SORT ET SORTDIRECTION AVEC UN GRID... par jesusonline
Source avec Zip Source .NET (Dotnet) CUSTOM DATEPIKER DÉRIVANT DE COMPOSITECONTROL par fredzool
Source avec Zip UN MODULE POUR APPRENDRE SQL par Elmarzougui
Source avec Zip Source .NET (Dotnet) BOUTON QUI EMPECHE LE MULTI CLIC AVANT LA FIN DU TRAITEMENT.... par fredzool
Source avec Zip Source .NET (Dotnet) BOUTON AVEC CSS ET USERCONTROL WITH EVENT par fredzool

Commentaires et avis

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!

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

Commentaire de hotch5 le 01/09/2006 18:47:00

ca marche pas cette code en asp.net

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mars 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,733 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales