Salut !
J'ai un problème de cast dans une requête et je ne vois plus trop quoi faire !
Dans cette requête, je récupère les demandes client associées à un compte (entreprise) et dans cette requête, je fais la somme du nombre d'intervenants associés à la demande. Le problème intervient sur le type de retour de SUM en SQL. J'ai lu que dans le cas ou un Int32 est passé en paramètre de SUM, le type de retour est Bigint soit Int64.
J'ai donc paramétré mon code en fonction mais toujours le problème de cast.
Voici mon code :
La classe LigneDemande.cs
Le code de la méthode ou j'exécute la requête (à la base, avt d'avoir testé le type de retour de SUM) :
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 using System; using System.Collections.Generic; using System.Text; namespace Gescom07.Core { // DESCRIPTION : Classe métier représentant une ligne dans la liste des demandes public class LigneDemande { #region Attributs public const string DEMANDE = "ID"; private Int64 _demande; public Int64 Demande { get { return _demande; } set { _demande = value; } } public const string DATE_SAISIE = "DATE_CREATION"; private DateTime _dateSaisie; public DateTime DateSaisie { get { return _dateSaisie; } set { _dateSaisie = value; } } public const string COMPTE = "NOM_COMPTE"; private string _nomCompte; public string Compte { get { return _nomCompte; } set { _nomCompte = value; } } public const string CONTACT = "NOM_CONTACT"; private string _nomContact; public string Contact { get { return _nomContact; } set { _nomContact = value; } } public const string COMMENTAIRE = "COMMENTAIRE"; private string _commentaire; public string Commentaire { get { return _commentaire; } set { _commentaire = value; } } public const string NBINTER = "NBINTER"; private Int64 _nbInter; public Int64 NbInter { get { return _nbInter; } set { _nbInter = value; } } public const string OFFRE = "OFFRE"; private string _offre; public string Offre { get { return _offre; } set { _offre = value; } } public const string DATE_DEBUT = "DATE_DEBUT_CONTRAT"; private DateTime _dateDebut; public DateTime DateDebut { get { return _dateDebut; } set { _dateDebut = value; } } #endregion #region Constructeur public LigneDemande(){ } public LigneDemande(Int64 demande, DateTime saisie, string compte, string contact, Int64 nbinter, string offre, DateTime debut) { this.Demande = demande; this.DateSaisie = saisie; this.Compte = compte; this.Contact = contact; this.NbInter = nbinter; this.Offre = offre; this.DateDebut = debut; } #endregion } }
L'exception intervient à cet endroit :
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 // Récupération des demandes associées à un compte public IList<LigneDemande> GetDemandes(Int32 idCompte) { return AdoTemplate.QueryWithRowMapperDelegate<LigneDemande>(CommandType.Text, "SELECT D.ID, D.DATE_CREATION, CPT.NOM NOM_COMPTE, C.NOM NOM_CONTACT, D.COMMENTAIRE, SUM(R.NB_INTER) NBINTER, O.LIBELLE OFFRE, D.DATE_DEBUT_CONTRAT FROM DEMANDE D, RECHERCHE R, COMPTE CPT, CONTACT C, OFFRE O WHERE CPT.ID = " + idCompte + " AND C.ID_COMPTE = CPT.ID AND D.ID_CONTACT = C.ID AND R.ID_DEMANDE = D.ID AND D.CODE_OFFRE = O.CODE GROUP BY D.ID, D.DATE_CREATION, CPT.NOM, C.NOM, D.COMMENTAIRE, O.LIBELLE, D.DATE_DEBUT_CONTRAT", delegate(IDataReader dataReader, int rowNum) { LigneDemande line = new LigneDemande(); Type t = dataReader.GetOrdinal(LigneDemande.NBINTER).GetType(); // Type retourné = Int32 alors que c'est censé être Int64 line.Demande = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.DEMANDE)) ? 0 : dataReader.GetInt64(dataReader.GetOrdinal(LigneDemande.DEMANDE)); line.DateSaisie = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.DATE_SAISIE)) ? DateTime.MaxValue : (DateTime)dataReader.GetValue(dataReader.GetOrdinal(LigneDemande.DATE_SAISIE)); line.Compte = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.COMPTE)) ? string.Empty : dataReader.GetString(dataReader.GetOrdinal(LigneDemande.COMPTE)).TrimEnd(); line.Contact = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.CONTACT)) ? string.Empty : dataReader.GetString(dataReader.GetOrdinal(LigneDemande.CONTACT)).TrimEnd(); line.Commentaire = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.COMMENTAIRE)) ? string.Empty : dataReader.GetString(dataReader.GetOrdinal(LigneDemande.COMMENTAIRE)).TrimEnd(); line.NbInter = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.NBINTER)) ? 0 : dataReader.GetInt64(dataReader.GetOrdinal(LigneDemande.NBINTER)); // InvalidCastException ici line.Offre = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.OFFRE)) ? string.Empty : dataReader.GetString(dataReader.GetOrdinal(LigneDemande.OFFRE)).TrimEnd(); line.DateDebut = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.DATE_DEBUT)) ? DateTime.MaxValue : (DateTime)dataReader.GetValue(dataReader.GetOrdinal(LigneDemande.DATE_DEBUT)); return line; }); }
Comme le type retourné par SUM semble être Int32 j'ai modifié le code en conséquent mais toujours la même erreur de cast.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 line.NbInter = dataReader.IsDBNull(dataReader.GetOrdinal(LigneDemande.NBINTER)) ? 0 : dataReader.GetInt64(dataReader.GetOrdinal(LigneDemande.NBINTER));
Merci pour votre aide !
Partager