begin process at 2012 05 27 07:09:13
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

ASP.Net

 > NAVIGATION DANS UN DATASET ET AFFICHAGE DANS TEXTBOX

NAVIGATION DANS UN DATASET ET AFFICHAGE DANS TEXTBOX


 Information sur la source

Note :
Aucune note
Catégorie :ASP.Net Source .NET ( DotNet ) Niveau :Débutant Date de création :12/02/2003 Date de mise à jour :12/02/2003 19:11:02 Vu :13 217

Auteur : vieuxLion

Ecrire un message privé
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

C'est vrai qu'en ASP (pas NET) on pouvait facilement faire un MoveNext sur un Recordset... Avec le Dataset, c'est devenu impossible ... directement !

Sous WinForm, ce n'est pas trop grave car il existe le BindingContext et sa propriété Position , mais il est absent dans les WebForms

Voici ma solution qui demande à être critiquée et améliorée
inutile de lier un à un les contrôles par DataBind().

Soit une SqlConnection, un SqlDataAdapter et un Dataset
la connection est dans mon exemple sur Northwind (table Categories)
Nous avons deux TextBox dans lesquels on veut afficher des colonnes de l'enregistrement courant.
On veut pouvoir passer à l'enregistrement suivant avec un bouton ... "Suivant"


Dans le bouton "Suivant" coder simplement pour chaque contrôle TextBox :
TextBoxID.Text = dsCategories1.Tables["Categories"].Rows[ix]["Categ oryID"];
TextBoxName.Text = dsCategories1.Tables["Categories"].Rows[ix]["Categ oryName"];

ou ix est un membre statique de la WebForm
private static int ix=0;

On peut aussi ajouter un bouton "Précédent", "Premier", "Dernier" ...
si l'on est nostalgique des "navigateurs à la DAO ou ADODB"

Source

  • using System;
  • using System.Collections;
  • using System.ComponentModel;
  • using System.Data;
  • using System.Drawing;
  • using System.Web;
  • using System.Web.SessionState;
  • using System.Web.UI;
  • using System.Web.UI.WebControls;
  • using System.Web.UI.HtmlControls;
  • namespace Datas
  • {
  • /// <summary>
  • /// Description résumée de [!output SAFE_CLASS_NAME].
  • /// </summary>
  • public class WebForm1 : System.Web.UI.Page
  • {
  • protected System.Web.UI.WebControls.TextBox TextBox1;
  • protected System.Web.UI.WebControls.TextBox TextBox2;
  • protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
  • protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
  • protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
  • protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
  • protected System.Data.SqlClient.SqlConnection sqlConnection1;
  • protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
  • protected System.Web.UI.WebControls.Button Button1;
  • protected Datas.dsCategories dsCategories1;
  • private static int ix=0;
  • private void Page_Load(object sender, System.EventArgs e)
  • {
  • // Placer ici le code utilisateur pour initialiser la page
  • sqlDataAdapter1.Fill(dsCategories1);
  • // //TextBox1.DataBind();//inutile
  • // //TextBox2.DataBind();//inutile
  • }
  • #region Web Form Designer generated code
  • override protected void OnInit(EventArgs e)
  • {
  • //
  • // CODEGEN : Cet appel est requis par le Concepteur Web Form ASP.NET.
  • //
  • InitializeComponent();
  • base.OnInit(e);
  • }
  • /// <summary>
  • /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
  • /// le contenu de cette méthode avec l'éditeur de code.
  • /// </summary>
  • private void InitializeComponent()
  • {
  • this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
  • this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
  • this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
  • this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
  • this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
  • this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
  • this.dsCategories1 = new Datas.dsCategories();
  • ((System.ComponentModel.ISupportInitialize)(this.dsCategories1)).BeginInit();
  • //
  • // sqlSelectCommand1
  • //
  • this.sqlSelectCommand1.CommandText = "SELECT CategoryID, CategoryName, Description, Picture FROM Categories";
  • this.sqlSelectCommand1.Connection = this.sqlConnection1;
  • //
  • // sqlInsertCommand1
  • //
  • this.sqlInsertCommand1.CommandText = "INSERT INTO Categories(CategoryName, Description, Picture) VALUES (@CategoryName," +
  • " @Description, @Picture); SELECT CategoryID, CategoryName, Description, Picture " +
  • "FROM Categories WHERE (CategoryID = @@IDENTITY)";
  • this.sqlInsertCommand1.Connection = this.sqlConnection1;
  • this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
  • this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
  • this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"));
  • //
  • // sqlUpdateCommand1
  • //
  • this.sqlUpdateCommand1.CommandText = @"UPDATE Categories SET CategoryName = @CategoryName, Description = @Description, Picture = @Picture WHERE (CategoryID = @Original_CategoryID) AND (CategoryName = @Original_CategoryName); SELECT CategoryID, CategoryName, Description, Picture FROM Categories WHERE (CategoryID = @CategoryID)";
  • this.sqlUpdateCommand1.Connection = this.sqlConnection1;
  • this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
  • this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
  • this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"));
  • this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
  • this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
  • this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Int, 4, "CategoryID"));
  • //
  • // sqlDeleteCommand1
  • //
  • this.sqlDeleteCommand1.CommandText = "DELETE FROM Categories WHERE (CategoryID = @Original_CategoryID) AND (CategoryNam" +
  • "e = @Original_CategoryName)";
  • this.sqlDeleteCommand1.Connection = this.sqlConnection1;
  • this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
  • this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
  • //
  • // sqlConnection1
  • //
  • this.sqlConnection1.ConnectionString = "data source=VIDAL;initial catalog=Northwind;integrated security=SSPI;persist secu" +
  • "rity info=False;workstation id=VIDAL;packet size=4096";
  • //
  • // sqlDataAdapter1
  • //
  • this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
  • this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
  • this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
  • this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
  • new System.Data.Common.DataTableMapping("Table", "Categories", new System.Data.Common.DataColumnMapping[] {
  • new System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"),
  • new System.Data.Common.DataColumnMapping("CategoryName", "CategoryName"),
  • new System.Data.Common.DataColumnMapping("Description", "Description"),
  • new System.Data.Common.DataColumnMapping("Picture", "Picture")})});
  • this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
  • //
  • // dsCategories1
  • //
  • this.dsCategories1.DataSetName = "dsCategories";
  • this.dsCategories1.Locale = new System.Globalization.CultureInfo("fr-FR");
  • this.dsCategories1.Namespace = "http://www.tempuri.org/dsCategories.xsd";
  • this.Button1.Click += new System.EventHandler(this.Button1_Click);
  • this.Load += new System.EventHandler(this.Page_Load);
  • ((System.ComponentModel.ISupportInitialize)(this.dsCategories1)).EndInit();
  • }
  • #endregion
  • private void Button1_Click(object sender, System.EventArgs e)
  • {
  • TextBox1.Text = dsCategories1.Tables["Categories"].Rows[ix]["CategoryID"].ToString();
  • TextBox2.Text = dsCategories1.Tables["Categories"].Rows[ix]["CategoryName"].ToString();
  • ix++;
  • }
  • }
  • }
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Datas
{
	/// <summary>
	/// Description résumée de [!output SAFE_CLASS_NAME].
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.TextBox TextBox2;
		protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
		protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
		protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
		protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
		protected System.Data.SqlClient.SqlConnection sqlConnection1;
		protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
		protected System.Web.UI.WebControls.Button Button1;
		protected Datas.dsCategories dsCategories1;
		private static int ix=0;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Placer ici le code utilisateur pour initialiser la page
			sqlDataAdapter1.Fill(dsCategories1);
//			//TextBox1.DataBind();//inutile
//			//TextBox2.DataBind();//inutile
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN : Cet appel est requis par le Concepteur Web Form ASP.NET.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
		/// le contenu de cette méthode avec l'éditeur de code.
		/// </summary>
		private void InitializeComponent()
		{    
			this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
			this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
			this.dsCategories1 = new Datas.dsCategories();
			((System.ComponentModel.ISupportInitialize)(this.dsCategories1)).BeginInit();
			// 
			// sqlSelectCommand1
			// 
			this.sqlSelectCommand1.CommandText = "SELECT CategoryID, CategoryName, Description, Picture FROM Categories";
			this.sqlSelectCommand1.Connection = this.sqlConnection1;
			// 
			// sqlInsertCommand1
			// 
			this.sqlInsertCommand1.CommandText = "INSERT INTO Categories(CategoryName, Description, Picture) VALUES (@CategoryName," +
				" @Description, @Picture); SELECT CategoryID, CategoryName, Description, Picture " +
				"FROM Categories WHERE (CategoryID = @@IDENTITY)";
			this.sqlInsertCommand1.Connection = this.sqlConnection1;
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"));
			// 
			// sqlUpdateCommand1
			// 
			this.sqlUpdateCommand1.CommandText = @"UPDATE Categories SET CategoryName = @CategoryName, Description = @Description, Picture = @Picture WHERE (CategoryID = @Original_CategoryID) AND (CategoryName = @Original_CategoryName); SELECT CategoryID, CategoryName, Description, Picture FROM Categories WHERE (CategoryID = @CategoryID)";
			this.sqlUpdateCommand1.Connection = this.sqlConnection1;
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Int, 4, "CategoryID"));
			// 
			// sqlDeleteCommand1
			// 
			this.sqlDeleteCommand1.CommandText = "DELETE FROM Categories WHERE (CategoryID = @Original_CategoryID) AND (CategoryNam" +
				"e = @Original_CategoryName)";
			this.sqlDeleteCommand1.Connection = this.sqlConnection1;
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
			// 
			// sqlConnection1
			// 
			this.sqlConnection1.ConnectionString = "data source=VIDAL;initial catalog=Northwind;integrated security=SSPI;persist secu" +
				"rity info=False;workstation id=VIDAL;packet size=4096";
			// 
			// sqlDataAdapter1
			// 
			this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
			this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
			this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
			this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																									  new System.Data.Common.DataTableMapping("Table", "Categories", new System.Data.Common.DataColumnMapping[] {
																																																					new System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"),
																																																					new System.Data.Common.DataColumnMapping("CategoryName", "CategoryName"),
																																																					new System.Data.Common.DataColumnMapping("Description", "Description"),
																																																					new System.Data.Common.DataColumnMapping("Picture", "Picture")})});
			this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
			// 
			// dsCategories1
			// 
			this.dsCategories1.DataSetName = "dsCategories";
			this.dsCategories1.Locale = new System.Globalization.CultureInfo("fr-FR");
			this.dsCategories1.Namespace = "http://www.tempuri.org/dsCategories.xsd";
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);
			((System.ComponentModel.ISupportInitialize)(this.dsCategories1)).EndInit();

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			TextBox1.Text = dsCategories1.Tables["Categories"].Rows[ix]["CategoryID"].ToString();
			TextBox2.Text = dsCategories1.Tables["Categories"].Rows[ix]["CategoryName"].ToString();
			ix++;
		}
	}
}



 Sources du même auteur

Source .NET (Dotnet) CRÉER UNE WEBFORM FRONTALE DE DONNÉE EN DEUX LIGNES [VS-NET ...

 Sources de la même categorie

Source avec Zip Source .NET (Dotnet) GUESTBOOK AVEC GRIDVIEW par DanMor498
Source avec Zip CHECKED DROPDOWNLIST par fredzool
Source avec Zip Source avec une capture Source .NET (Dotnet) GRIDVIEW WITH TREEVIEW AND CALLBACK par fredzool
Source avec Zip APPELLER UN WEBSERVICE DEPUIS JAVASCRIPT par fredzool
Source avec Zip Source .NET (Dotnet) MONEY TEXTBOX WITH EMBEDED JAVASCRIPT par fredzool

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
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,905 sec (3)

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