Salut !
J'ai un petit souci avec un cast.
En fait, dans ma fonction "button1_Click", je veux caster la SelectedValue d'une ListBox en int.
Et j'ai bien défini aupravant que :
Code : Sélectionner tout - Visualiser dans une fenêtre à part Ville_Id = (int)listBox1.SelectedValue;
Bref, je vois pas pourquoi ça cloche...
Code : Sélectionner tout - Visualiser dans une fenêtre à part listBox1.ValueMember = "Ville_Id";
Voici le code complet de ma Winform :
Si vous pouvez m'aider...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 namespace Habitat { public partial class Habite : Form { private Personne parent; public Habite(Personne pers) { InitializeComponent(); this.parent = pers; } private void button5_Click(object sender, EventArgs e) { this.Close(); } private void listHabitant() { ... } private void remplitListBox1() { SqlConnection objConnection; SqlCommand objCommand = new SqlCommand(); DataSet objDataset = new DataSet(); SqlParameter objParam; SqlDataAdapter objAdapter = new SqlDataAdapter(); string sConnection; int Pers_Id = (int)parent.listBox1.SelectedValue; objConnection = new SqlConnection(); sConnection = "Data Source=PC-MIKE\\SQLEXPRESS; Database=Habitat;UID=Mike_Ephec;PWD=*****"; objConnection.ConnectionString = sConnection; objCommand.Connection = objConnection; objCommand.CommandType = CommandType.StoredProcedure; objCommand.CommandText = "SP_APPL.ListAllVilleParPersonne"; objParam = new SqlParameter("@Pers_Id", Pers_Id); objCommand.Parameters.Add(objParam); try { objConnection.Open(); objAdapter.SelectCommand = objCommand; objAdapter.Fill(objDataset, "ListAllVilleParPersonne"); listBox1.DisplayMember = "Libelle"; listBox1.ValueMember = "Ville_Id"; listBox1.DataSource = objDataset.Tables[0].DefaultView; objConnection.Close(); } catch (SqlException ex) { MessageBox.Show(ex.ErrorCode.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message); throw ex; } } private void remplitListBox2() { ... } private void Habite_Load(object sender, EventArgs e) { listHabitant(); remplitListBox1(); remplitListBox2(); } private void button1_Click(object sender, EventArgs e) { SqlConnection objConnection; SqlCommand objCommand = new SqlCommand(); SqlParameter objParam; string sConnection; int Ville_Id; int nbreModif; Ville_Id = (int)listBox1.SelectedValue; objConnection = new SqlConnection(); sConnection = "Data Source=PC-MIKE\\SQLEXPRESS; Database=Habitat;UID=Mike_Ephec;PWD=*****"; objConnection.ConnectionString = sConnection; objCommand.Connection = objConnection; objCommand.CommandType = CommandType.StoredProcedure; objCommand.CommandText = "SP_APPL.N'HabitePas"; objParam = new SqlParameter("@Ville_Id", Ville_Id); objCommand.Parameters.Add(objParam); try { objConnection.Open(); nbreModif = objCommand.ExecuteNonQuery(); objConnection.Close(); } catch (SqlException ex) { MessageBox.Show(ex.ErrorCode.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message); throw ex; } remplitListBox1(); remplitListBox2(); } } }
Partager