Relation entre plusieurs combox lier a sqlserveur

mywman Messages postés 38 Date d'inscription mercredi 20 janvier 2010 Statut Membre Dernière intervention 19 mars 2015 - 3 mars 2014 à 08:12
mywman Messages postés 38 Date d'inscription mercredi 20 janvier 2010 Statut Membre Dernière intervention 19 mars 2015 - 10 mars 2014 à 13:35
Bonjour,

je suis entrain de dévélopper une application de gestion d'affectation en c#. Dans l'application on doit pouvoir affecter un agent dans un pays, sachant qu'un pays est constitué de villes, et que une ville est constitué d'arrondissement, je voudrais que lorsqu'on selectionne un pays du combobox pays, le combobox ville se rempli des villes de se pays. et lorsqu'on sélection un élément du combobox ville le combobox arrondissement se rempli des arrondissement concernant cette ville. Voici un code que j'ai fais mais sa coince, j'ai une exception.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.Common;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace dataset
{
public partial class Form1 : Form
{


DataSet dsMain1 = new DataSet();
DataSet dsMain2 = new DataSet();

string mychaine = "";
string myprovider = "";

public Form1(string mychaine, string myprovider)
{
InitializeComponent();


this.mychaine = mychaine;
this.myprovider = myprovider;



}

private void Form1_Load(object sender, EventArgs e)
{
InitDatabase();
}


private void InitDatabase()
{

// MyDbConnection = myDbProviderUtilities.CreateConnection(myprovider, mychaine);
// lib_Groupe = myDbProviderUtilities.AjouterQuote(lib_Groupe);


string sConnectionString;
sConnectionString ="Data Source=DMS;Initial Catalog=BD_DMS;User ID=sa;Password=dms";
/*"Password=dms;User ID=sa;"
+ "Initial Catalog=BD_DMS;"
+ "Data Source=DMS)";*/
SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();

SqlDataAdapter daPAYS
= new SqlDataAdapter("Select * From PAYS", objConn);
DataSet dsMain = new DataSet("DMS");
daPAYS.FillSchema(dsMain, SchemaType.Source, "PAYS");
daPAYS.Fill(dsMain, "PAYS");

DataTable tbPAYS;
tbPAYS = dsMain.Tables["PAYS"];

SqlDataAdapter daPROVINCE
= new SqlDataAdapter("Select * From PROVINCE", objConn);
DataSet dsMain1 = new DataSet("DMS");
daPAYS.FillSchema(dsMain1, SchemaType.Source, "PROVINCE");
daPAYS.Fill(dsMain1, "PROVINCE");



DataTable tbPROVINCE;
tbPROVINCE = dsMain.Tables["PROVINCE"];

// dsMain.Tables.Add(tbPAYS);
//dsMain.Tables.Add(tbPROVINCE);

// DataTable dt1 = new DataTable("PAYS");
//tbPAYS.Columns.Add(" ", typeof(string));
// tbPAYS.Columns.Add(" ", typeof(string));
/*
dt1.Rows.Add(new object[] { "1", "france" });
dt1.Rows.Add(new object[] { "2", "Italie" });
dt1.Rows.Add(new object[] { "3", "Espagne" });
dt1.Rows.Add(new object[] { "4", "Allemagne" });
dsMain.Tables.Add(dt1);
*/
// DataTable dt2 = new DataTable("PROVINCE");
// tbPROVINCE.Columns.Add("ID_PROV", typeof(string));
// tbPROVINCE.Columns.Add("LIB_PROV", typeof(string));
// tbPROVINCE.Columns.Add("ID_PAYS", typeof(string));
/*
dt2.Rows.Add(new object[] { "1", "Haute garonne", "1" });
dt2.Rows.Add(new object[] { "2", "Ariege", "1" });
dt2.Rows.Add(new object[] { "3", "Paris", "1" });
dt2.Rows.Add(new object[] { "4", "Rome", "2" });
dt2.Rows.Add(new object[] { "5", "Naples", "2" });
dt2.Rows.Add(new object[] { "6", "Madrid", "3" });
dt2.Rows.Add(new object[] { "7", "Barcelone", "3" });
dt2.Rows.Add(new object[] { "8", "Munich", "4" });
dt2.Rows.Add(new object[] { "9", "Baden Baden", "4" });
dt2.Rows.Add(new object[] { "10", "Ulm", "4" });

dsMain.Tables.Add(dt2);
*/

dsMain.Relations.Add(new DataRelation("Membership", dsMain.Tables["PAYS"].Columns["ID_PAYS"], dsMain.Tables["PROVINCE"].Columns["ID_PAYS"]));
/*
// Create dummy rows for any parent that has no children
foreach (DataRow Row in dt1.Rows)
{
if (Row.GetChildRows("Membership").Length == 0)
{
DataRow s = dt2.NewRow();
s["ID_PROV"] = "";
s["LIB_PROV"] = "";
s["ID_PAYS"] = Row["ID_PAYS"];
dt2.Rows.Add(s);
}
}
*/
// dsMain.AcceptChanges();

cboNations.DataSource = dsMain.Tables["PAYS"];
cboNations.ValueMember = "PAYS.ID_PAYS"; // L'exception c'est a ce niveau "Impossible de créer une liste enfant pour le champ PAYS."/B
cboNations.DisplayMember = "PAYS.LIB_PAYS"; // L'exception c'est a ce niveau "Impossible de créer une liste enfant pour le champ PAYS."/B

cboStates.DataSource = dsMain.Tables["PROVINCE"];
cboStates.ValueMember = "PAYS.Membership.ID_PROV";
cboStates.DisplayMember = "PAYS.Membership.LIB_PROV";


}


}
}


A la ligne 112, j'ai une exception: "La référence d'objet n'est pas définie à une instance d'un objet".

merci d'avance pour les propositions

3 réponses

DevLama Messages postés 356 Date d'inscription mercredi 13 avril 2011 Statut Membre Dernière intervention 18 novembre 2021 10
3 mars 2014 à 13:35
Salut,ton code fonctionne différemment de ce que tu dis,
Normalement tu ne devrais pas charger tous les combos lors du chargement de la page.
Exemple:
Le combo Pays doit être charger 1erement puis lorsque tu fais selectTextChanged,tu vérifies si le combox n'est pas vide après avoir changer le contenu,alors le combo Ville devient actif(Enabled=true) et dans la code de l'événement DropDown tu vas faire une recherche sur les villes par rapport au pays sélectionner.
et tu en fais autant pour la suite.
/*********************FIN EXEMPLE*****************
bon à part ça,pourquoi tu charges les données lors du chargement de ton formulaire sans utiliser l'Asynchrone? si tu as des données assez sérieuses(beaucoup) tu auras un problème lors de l'exécution de ton application.
1
mywman Messages postés 38 Date d'inscription mercredi 20 janvier 2010 Statut Membre Dernière intervention 19 mars 2015
10 mars 2014 à 13:35
Bjr les gars,

j'ai fait comme vous m'avez indiqué.j'ai réussi à partir du combobox cBx_PAYS_AFFECTATION à charger le combox cBx_PROVINCE_AFFECTATION. Mais le problème c'est que lorsque je veux sélectionne un élément de cBx_PROVINCE_AFFECTATION pour charger le combox cBx_VILLE_AFFECTATION le selectedValue ne reçoit pas de valeur.

Je vous montre mon code, je développe en 3tiers:

UserInterface:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DMS
{
public partial class FmAffectation : Form
{

BLL.PaysMetier ScPays = null;
BLL.ProvinceMetier ScProvince = null;


string mychaine = "";
string myprovider = "";

public FmAffectation(string mychaine, string myprovider)
{
InitializeComponent();

this.mychaine = mychaine;
this.myprovider = myprovider;

ScPays = new BLL.PaysMetier(mychaine, myprovider);
ScProvince = new BLL.ProvinceMetier(mychaine, myprovider);
}

private void FmAffectation_Load(object sender, EventArgs e)
{
ScPays.RemplirComboBoxPays(cBx_PAYS_AFFECTATION, "");

}

private void btn_Fermer_version_Click(object sender, EventArgs e)
{
this.Close();
}

private void cBx_PAYS_AFFECTATION_SelectedIndexChanged(object sender, EventArgs e)
{
if (cBx_PAYS_AFFECTATION.SelectedIndex > -1)
{

ScProvince.chargerProvince(cBx_PAYS_AFFECTATION.SelectedValue.ToString(), cBx_PROVINCE_AFFECTATION);

}
else
{

}

}

private void cBx_PROVINCE_AFFECTATION_SelectedIndexChanged(object sender, EventArgs e)
{
if (cBx_PROVINCE_AFFECTATION.SelectedIndex > -1)
{
ScProvince.chargerVille(cBx_PROVINCE_AFFECTATION.SelectedValue.ToString(), cBx_VILLE_AFFECTATION);
}
else
{
}

}

private void cBx_VILLE_AFFECTATION_SelectedIndexChanged(object sender, EventArgs e)
{

}

}
}




Couche d'accès au données:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DAL
{

public class ProvinceData
{

string mychaine = "";
string myprovider = "";

public ProvinceData (string mychaine, string myprovider)
{
this.mychaine = mychaine;
this.myprovider = myprovider;
}

public int chargerProvince(String comboProv, ComboBox cBx_PROVINCE_AFFECTATION)
{
try
{
string sConnectionString;
sConnectionString = "Data Source=ST02;Initial Catalog=BD_DMS;User ID=sa;Password=dms";

SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();

DataSet dsMain = new DataSet("DMS");
SqlDataAdapter daPROVINCE
= new SqlDataAdapter("Select LIB_PROV From PROVINCE WHERE ID_PAYS = '"+ comboProv +"'", objConn);
daPROVINCE.FillSchema(dsMain, SchemaType.Source, "PROVINCE");
daPROVINCE.Fill(dsMain, "PROVINCE");

DataTable tbPROVINCE;
tbPROVINCE = dsMain.Tables["PROVINCE"];

cBx_PROVINCE_AFFECTATION.DataSource = dsMain.Tables["PROVINCE"];
cBx_PROVINCE_AFFECTATION.DisplayMember = "LIB_PROV";

return 0;

}
catch
{
return -1;
}
}

public int chargerVille(String comboVille, ComboBox cBx_VILLE_AFFECTATION)
{
try
{
string sConnectionString;
sConnectionString = "Data Source=ST02;Initial Catalog=BD_DMS;User ID=sa;Password=dms";

SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();

DataSet dsMain = new DataSet("DMS");
SqlDataAdapter daVILLE
= new SqlDataAdapter("Select LIB_VILLE From PROVINCE WHERE ID_PROV = '" + comboVille + "'", objConn);
daVILLE.FillSchema(dsMain, SchemaType.Source, "VILLE");
daVILLE.Fill(dsMain, "VILLE");

DataTable tbPROVINCE;
tbPROVINCE = dsMain.Tables["VILLE"];

cBx_VILLE_AFFECTATION.DataSource = dsMain.Tables["VILLE"];
cBx_VILLE_AFFECTATION.DisplayMember = "LIB_VILLE";

return 0;

}
catch
{
return -1;
}
}
}
}


Couche Métier:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Windows.Forms;

namespace BLL
{
public class ProvinceMetier
{

DAL.ProvinceData ScProvince = null;


string mychaine = "";
string myprovider = "";

public ProvinceMetier(string mychaine, string myprovider)
{
this.mychaine = mychaine;
this.myprovider = myprovider;

ScProvince = new DAL.ProvinceData(mychaine, myprovider);
}


public int chargerProvince(String comboProv, ComboBox cBx_PROVINCE_AFFECTATION)
{
try
{
int valeurRetour = 1;

valeurRetour = ScProvince.chargerProvince(comboProv, cBx_PROVINCE_AFFECTATION);

return valeurRetour;

}
catch
{
return -1;
}
}

public int chargerVille(String comboVille, ComboBox cBx_VILLE_AFFECTATION)
{
try
{
int valeurRetour = 1;

valeurRetour = ScProvince.chargerProvince(comboVille,cBx_VILLE_AFFECTATION);

return valeurRetour;

}
catch
{
return -1;
}
}

}
}




Merci les gars pour votre aide
0
M@lick Messages postés 1 Date d'inscription lundi 3 mars 2014 Statut Membre Dernière intervention 3 mars 2014
3 mars 2014 à 10:23
Tu doit créer une requête préparer au comboboxPays SelectedIndexChange du genre select Nomville from Ville where nomPays ="'" & comboboxPays.selectedItem & "'"
Je ne sais pas si je me suis bien fait comprendre
0
mywman Messages postés 38 Date d'inscription mercredi 20 janvier 2010 Statut Membre Dernière intervention 19 mars 2015
3 mars 2014 à 15:37
Merci, les gars pour les propositions de solutions.Je teste et je vous fais part demain. merci les gars
0
Rejoignez-nous