IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Détails de l'exception: System.FormatException: Le format de la chaîne d'entrée est incorrect. [Débutant]


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Aprenti
    Inscrit en
    Mai 2015
    Messages
    199
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Aprenti

    Informations forums :
    Inscription : Mai 2015
    Messages : 199
    Par défaut Détails de l'exception: System.FormatException: Le format de la chaîne d'entrée est incorrect.
    Bonjour,
    Ma variable, 'pourcentage_taxe1' et 'pourcentage_taxe2'se trouvent dans plusieurs fichiers.
    Ces taux sont variables par semaine et je dois le changer chaque semaine dans plusieurs fichiers.
    J'aimerais bien faire de changements, mais dans un seul fichier pour tous les formulaires.
    J'essaie de les mettre dans le fichier web.config
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    <appSettings>
        <add key="pourcentage_taxe1" value="0.05m" />
        <add key="pourcentage_taxe2" value="0.07m" />
    </appSettings>
    et je change le code en c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    //private decimal pourcentage_taxe1 = 0.05m;
    static string string_pourcentage_taxe1 = ConfigurationManager.AppSettings["pourcentage_taxe1"];
    private decimal pourcentage_taxe1 = decimal.Parse(string_pourcentage_taxe1);
    Et j'ai le message
    Détails de l'exception: System.FormatException: Le format de la chaîne d'entrée est incorrect.
    Voici mes codes :
    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
     
    namespace Ecom
    {
    	public partial class engregistrement : System.Web.UI.Page
    		{
    			//private decimal pourcentage_taxe1 = 0.05m;
    			static string string_pourcentage_taxe1 = ConfigurationManager.AppSettings["pourcentage_taxe1"];
    			private decimal pourcentage_taxe1 = decimal.Parse(string_pourcentage_taxe1);
     
     
    			//private decimal pourcentage_taxe2 = 0.07m;
    			static string string_pourcentage_taxe2 = ConfigurationManager.AppSettings["pourcentage_taxe2"];
    			private decimal pourcentage_taxe2 = decimal.Parse(string_pourcentage_taxe2);
     
     
     
     
    			 private void taxeCalculer(string xMontantTotal, string xCuntry, string xState, string xNombre)
    				{
     
    					calculMontant = Convert.ToDecimal(xMontantTotal);
     
    					Nombre = Convert.ToDecimal(xNombre);
     
    					calculMontant = calculMontant * Nombre;
     
     
    					if (xCuntry == "CA")
    					{
    						switch (xState)
    						{
    							case "QC":
     
    								T1erTaxe = calculMontant * pourcentage_taxe2;
    								T1erTaxe = decimal.Round(T1erTaxe, 2, MidpointRounding.AwayFromZero);
     
     
    								T2eTaxe = calculMontant * pourcentage_taxe1;
    								T2eTaxe = decimal.Round(T2eTaxe, 2, MidpointRounding.AwayFromZero);
     
    								deuxTaxes = T1erTaxe + T2eTaxe;
    								Somme = calculMontant + T1erTaxe + T2eTaxe;
    								break;
     
    							default:
     
    								T1erTaxe = calculMontant * pourcentage_taxe2;
    								T1erTaxe = decimal.Round(T1erTaxe, 2, MidpointRounding.AwayFromZero);
     
    								deuxTaxes = T1erTaxe;
    								Somme = calculMontant + T1erTaxe;
    								break;
     
    						}
    					}
    					else
    					{
    						deuxTaxes = T1erTaxe + T2eTaxe;
    						Somme = calculMontant;
    					}
     
     
    					intSomme = Convert.ToInt32(Somme * 100);
     
     
    				}
     
     
    			protected void Button_Click(object sender, System.EventArgs e)
    				{
    					object recup_info = Session["paiePrivatePage"];
    					paiement.paiePrivatePageState paiem = (paiement.nouveau1UtilisateurInscriptionPrivatePageState)recup_info;
     
    					CalculTaxe(paiem.PriceAmt.ToString(), paiem.CountryShipping, paiem.StateShipping, paiem.NumberOfCopies.ToString());
     
    					string stringDB = ConfigurationManager.AppSettings["stringDB"];
     
    					MySqlConnection conn = new MySqlConnection(stringDB);
     
    					conn.Open();
     
    					string req = ConfigurationManager.AppSettings["reqSql"];
     
     
    					MySqlCommand cmd = new MySqlCommand(req, conn);
     
     
     
    					MySqlParameter tps = new MySqlParameter("@tps", T1erTaxe);
    					MySqlParameter tvq = new MySqlParameter("@tvq", T2eTaxe);
     
     
    					//MySqlParameter somme = new MySqlParameter("@somme", Somme.ToString());
    					MySqlParameter somme = new MySqlParameter("@somme", decimal.Parse(Somme.ToString()));
     
    					cmd.Parameters.Add(tps);
    					cmd.Parameters.Add(tvq);
     
    					cmd.Parameters.Add(somme);
     
    					cmd.ExecuteNonQuery();
    					conn.Close();
    				}
     
     
    	 }
    }
    Est-ce que vous avez une idée pour régler la 1re partie de mon code où il y a cette erreur ?
    Sinon comment je peux la mettre la valeur dans web.config et le récupérer en décimale dans mes formulaires ?

    Merci

  2. #2
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Les éléments dans le fichier web.config sont de type string.

    Pour sauvegarder correctement un décimal qui vaut 0.5m tu doit enregistrer dans le web.config un string qui vaut 0,5
    Ce dernier pourra être reconverti dans la valeur décimal correspondante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
                decimal d = 0.5m;
                string t = d.ToString(); // t vaut 0,5
                decimal f = Decimal.Parse(t); // f vaut 0.5m

  3. #3
    Membre éclairé
    Homme Profil pro
    Aprenti
    Inscrit en
    Mai 2015
    Messages
    199
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Aprenti

    Informations forums :
    Inscription : Mai 2015
    Messages : 199
    Par défaut
    Bonjour Eric(lm128),

    Merci pour tes aides,
    J'ai changé web.config comme tu m'as dit, sans suffixe "m" :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <appSettings>
        <add key="pourcentage_taxe1" value="0.05" />
        <add key="pourcentage_taxe2" value="0.07" />
    </appSettings>
    et voici ce que je fais en c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    static string string_pourcentage_taxe1 = ConfigurationManager.AppSettings["pourcentage_taxe1"];
    private decimal pourcentage_taxe1 = decimal.Parse(string_pourcentage_taxe1);
    et voici je que je vois comme (même) message dans le navigateur :

    Détails de l'exception: System.FormatException: Le format de la chaîne d'entrée est incorrect.
    À votre avis, où il y a quelque chose qui manque ?

    Merci

  4. #4
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Et si tu essai de mettre la même syntaxe que celle que je t'ai transmise ?
    C'est à dire une virgule au lieu d'un point ?

    Pour sauvegarder correctement un décimal qui vaut 0.5m tu doit enregistrer dans le web.config un string qui vaut 0,5
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <add key="pourcentage_taxe1" value="0.05" />

  5. #5
    Membre éclairé
    Homme Profil pro
    Aprenti
    Inscrit en
    Mai 2015
    Messages
    199
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Aprenti

    Informations forums :
    Inscription : Mai 2015
    Messages : 199
    Par défaut
    Bonjour Eric(lm128),

    Youpiii ça marche avec les virgules

    Merci pour ton aide!

    dans 'appSettings' de 'web.config'

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <appSettings>
        <add key="pourcentage_taxe1" value="0,05" />
        <add key="pourcentage_taxe2" value="0,07" />
    </appSettings>
    et dans C#
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    static string string_pourcentage_taxe1 = ConfigurationManager.AppSettings["pourcentage_taxe1"];
    private decimal pourcentage_taxe1 = decimal.Parse(string_pourcentage_taxe1);
    merci encore

  6. #6
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Tant mieux, question : est ce que ton projet est en asp.net ?
    Il ne me semble pas que j'ai nativement accès à la classe ConfigurationManager que je ne connais pas.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] System.FormatException: Le format de la chaîne d'entrée est incorrect
    Par white_mind dans le forum ASP.NET
    Réponses: 1
    Dernier message: 30/08/2013, 21h28
  2. Le format de la chaîne d'entrée est incorrect.
    Par Josselin54 dans le forum C#
    Réponses: 10
    Dernier message: 26/01/2011, 08h13
  3. Le format de la chaîne d'entrée est incorrect.
    Par badi3a82 dans le forum ASP.NET
    Réponses: 5
    Dernier message: 21/10/2010, 12h07
  4. Réponses: 2
    Dernier message: 12/02/2009, 09h59
  5. [C#] [1.1] Le format de la chaîne d'entrée est incorrect
    Par Sup@Lou dans le forum Windows Forms
    Réponses: 2
    Dernier message: 08/08/2006, 10h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo