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

Visual Studio Discussion :

Probleme avec app.config


Sujet :

Visual Studio

  1. #1
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut Probleme avec app.config
    Bonjour, je suis débutante en VS, j'ai crée un petit projet windows console ou j'ai crée un fichier app.config contenant :

    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
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     
     
    	<configSections>
    		<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    			<section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    		</sectionGroup>
    	</configSections>
     
    	<appSettings>
    		<add key="conString" value="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=CNEH_HOMOLOGATION;"/>
    	</appSettings>
     
    </configuration>
    et dans mon form j'ai mis un boutton et :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    private void button1_Click(object sender, EventArgs e)
            {
                System.Configuration.AppSettingsReader cfg = new System.Configuration.AppSettingsReader();
                string scon = cfg.GetValue("conString", typeof(System.String)).ToString();
            }
    mais je récupere toujours null au niveau de mon cfg et bien sur scon aussi, j'ai pris le meme projet et je l'ai testé chez un ami qui a vs2008 (le mien c'est 2005) et j'ai pu récupéré le cfg et le scon, vous avez une idée ?

  2. #2
    Rédacteur
    Avatar de Louis-Guillaume Morand
    Homme Profil pro
    Cloud Architect
    Inscrit en
    Mars 2003
    Messages
    10 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Cloud Architect
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2003
    Messages : 10 839
    Points : 28 252
    Points
    28 252
    Par défaut
    que c'est complexe toutes ces lignes
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    cfg.GetValue("conString", typeof(System.String)).ToString();
    ici, tu prends une string dans le fichier, tu la cast en objet String puis tu la transformes en String avant de la mettre dans une variable String. t'u crois pas que c'est p-e un peu trop?



    teste ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string machaine = ConfigurationManager.AppSettings["conString"];
    ps: tu es sûre que quand tu compiles, le fichier app.config change bien de nom?
    si ton appli se nomme test.exe alors le fichier de config se nomme test.exe.config. le changement de nom se fait à la compilation logiquement et c'est Visual qui s'en charge, SI et SEULEMENT SI, tu as ajouté le fichier app.config au projet.
    moi c'est Louis-Guillaume, ni Louis, ni Guillaume mais Louis-Guillaume et je n'aide pas ceux qui écorchent mon nom

  3. #3
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut
    lorsque j'ai essayé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     string machaine = ConfigurationManager.AppSettings["conString"];
    il me dit que Configuration manager n'existe pas dans le context. pour app.config ca existe au niveau du projet lorsque je débugg je vois pas de changement de noms

  4. #4
    Rédacteur
    Avatar de Louis-Guillaume Morand
    Homme Profil pro
    Cloud Architect
    Inscrit en
    Mars 2003
    Messages
    10 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Cloud Architect
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2003
    Messages : 10 839
    Points : 28 252
    Points
    28 252
    Par défaut
    1- pour l'espace de nom, il suffit de rajouter la référence à System.Configuration


    2- dans visual Studio, le fichier DOIT s'appeller app.config dans l'arbo du projet

    3- dans le dossier debug/release, APRES compilation, le fichier doit avoir changé de nom
    moi c'est Louis-Guillaume, ni Louis, ni Guillaume mais Louis-Guillaume et je n'aide pas ceux qui écorchent mon nom

  5. #5
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut
    Merci beaucoup Louis-Guillaume Morand ca marche, je vais l'essayer maintenant sur mon projet principal

  6. #6
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut
    au niveau de mon projet ca marche pas, j'ai une solution contenant trois projet (2 bibliothèque de classes et 1 application windows), je travaille avec sqlclient :
    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
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    using System;
    using System.Configuration;
     
    namespace CNEH.DataAbstractionLayer.BData.Data
     
    {
        public class SqlClient
        {
            public static string m_ConnectionString;
            System.Data.SqlClient.SqlCommand m_SqlCommand;
            System.Data.SqlClient.SqlConnection m_SqlConnection;
            System.Data.SqlClient.SqlDataReader m_SqlDataReader;
     
            #region CONSTRUCTEURS
     
            /// <summary>
            /// est le constructeur par défaut
            /// </summary>
            public SqlClient()
            {
                m_ConnectionString = ReadConString();
            }
            #endregion
     
            private string ReadConString()
            {
                string scon = ConfigurationManager.AppSettings["conString"];
                //System.Configuration.AppSettingsReader cfg = new System.Configuration.AppSettingsReader();
                //string scon = cfg.GetValue("conString", typeof(System.String)).ToString();
                return scon;
            }
     
            #region METHODES
     
            #region Create COMMAND
     
            /// <summary>
            /// permet de créer une command qui pourra par la suite soit charger une liste(executeReader),soit une valeur(executeScalar),soit écrire dans la base avec insert,update,delete,etc. (executeNonQuery)
            /// </summary>
            /// <param name="oCommandType">CommandType(Text,Storedprocedure ou TableDirect)</param>
            /// <param name="sCommandText">le nom de la procédure stockée ou la requte SQL</param>
            /// <returns>objet command crée</returns>
            public System.Data.SqlClient.SqlCommand CreateCommand(System.Data.CommandType oCommandType, string sCommandText)
            {
                try
                {
                    if (m_SqlCommand == null)
                    {
                        m_SqlCommand = new System.Data.SqlClient.SqlCommand();
                    }
                    else
                    {
                        m_SqlCommand = null;
                        m_SqlCommand = new System.Data.SqlClient.SqlCommand();
                    }
     
                    m_SqlCommand.Connection = CreateConnection();
                    m_SqlCommand.CommandType = oCommandType;
                    m_SqlCommand.CommandText = sCommandText;
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                return m_SqlCommand;
     
            }
     
            #endregion
     
            #region CONNECTION
     
            /// <summary>
            /// permet de créer la connexion
            /// </summary>
            /// <returns>la connexion</returns>
            private System.Data.SqlClient.SqlConnection CreateConnection()
            {
                try
                {
                    if (m_SqlConnection == null)
                    {
                        m_SqlConnection = new System.Data.SqlClient.SqlConnection();
     
                        if (SqlClient.m_ConnectionString == string.Empty)
                        {
                            throw new Exception("Chaine de connexion SQL Server non renseignée");
                        }
                        else
                        {
                            m_SqlConnection.ConnectionString = SqlClient.m_ConnectionString;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                return m_SqlConnection;
            }
            /// <summary>
            /// ouvre la connection
            /// </summary>
            /// <returns>vrai si la connection a pu étre ouverte,faux dans les autres cas</returns>
            public bool OuvrirConnection()
            {
                bool bResultat;
     
                bResultat = true;
     
                try
                {
                    if (m_SqlConnection != null)
                    {
                        if (m_SqlConnection.State == System.Data.ConnectionState.Closed)
                        {
                            m_SqlConnection.Open();
                        }
                        else
                        {
                            bResultat = false;
                        }
                    }
                    else
                    {
                        bResultat = false;
                    }
                }
                catch (System.Exception ex)
                {
                    bResultat = false;
                    throw ex;
                }
                return bResultat;
            }
            /// <summary>
            /// But : Fermer la connection
            /// </summary>
            /// <returns>vrai si la connection a été fermée,faux dans les autres cas</returns>
            public bool CloseConnection()
            {
                bool bResultat;
     
                bResultat = true;
     
                try
                {
                    if (m_SqlConnection != null)
                    {
                        m_SqlConnection.Close();
                    }
                    else
                    {
                        bResultat = false;
                    }
                }
                catch (System.Exception ex)
                {
                    bResultat = false;
                    throw ex;
                }
                return bResultat;
            }
            /// <summary>
            /// But : retourner l'etat de la connexion
            /// </summary>
            /// <returns></returns>
            public System.Data.ConnectionState EtatConnection()
            {
                System.Data.ConnectionState oConnectionState;
     
                oConnectionState = new System.Data.ConnectionState();
                try
                {
                    if (m_SqlConnection == null)
                    {
     
                    }
                    else
                    {
                        switch (m_SqlConnection.State)
                        {
                            case System.Data.ConnectionState.Broken:
                                oConnectionState = System.Data.ConnectionState.Broken;
                                break;
                            case System.Data.ConnectionState.Closed:
                                oConnectionState = System.Data.ConnectionState.Closed;
                                break;
                            case System.Data.ConnectionState.Connecting:
                                oConnectionState = System.Data.ConnectionState.Connecting;
                                break;
                            case System.Data.ConnectionState.Executing:
                                oConnectionState = System.Data.ConnectionState.Executing;
                                break;
                            case System.Data.ConnectionState.Fetching:
                                oConnectionState = System.Data.ConnectionState.Fetching;
                                break;
                            case System.Data.ConnectionState.Open:
                                oConnectionState = System.Data.ConnectionState.Open;
                                break;
                        }
                    }
     
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                return oConnectionState;
            }
     
            #endregion
     
            #region PARAMETRE
     
            /// <summary>
            /// ajoute un parametre à la command
            /// </summary>
            /// <param name="sParameterName">le nom du paramétre (ex: "@code_type")</param>
            /// <param name="oSqlDbType">le type de données (ex:SqlDbType.VarChar)</param>
            /// <param name="oValue">la valeur de ce paramétre (cela peut étre un string,un int,etc.) c'est pourquoi le type de cet argument est défini à object</param>
            public void AddCommandParameter(string sParameterName, System.Data.SqlDbType oSqlDbType, object oValue)
            {
                System.Data.SqlClient.SqlParameter oSqlParameter;
     
                try
                {
                    oSqlParameter = CreateParametre(sParameterName, oSqlDbType, oValue);
                    this.m_SqlCommand.Parameters.Add(oSqlParameter);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            /// crée le paramétre pour la command
            /// </summary>
            /// </summary>
            /// <param name="sParameterName">le nom du paramétre (ex: "@code_type")</param>
            /// <param name="oSqlDbType">le type de données (ex:SqlDbType.VarChar)</param>
            /// <param name="oValue">la valeur de ce paramétre (cela peut étre un string,un int,etc.) c'est pourquoi le type de cet argument est défini à object</param>
            /// <returns>le paramétre crée</returns>
            private System.Data.SqlClient.SqlParameter CreateParametre(string sParameterName, System.Data.SqlDbType oSqlDbType, object oValue)
            {
                System.Data.SqlClient.SqlParameter oSqlParameter;
     
                oSqlParameter = new System.Data.SqlClient.SqlParameter();
     
                try
                {
                    oSqlParameter.ParameterName = sParameterName;
                    oSqlParameter.SqlDbType = oSqlDbType;
                    oSqlParameter.Value = oValue;
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
     
                return oSqlParameter;
            }
     
            #endregion
     
            #region DATAREADER
     
            /// <summary>
            /// But : fermer le DataReader
            /// </summary>
            /// <returns>vrai si le DataReader a été fermé,faux dans les autres cas</returns>
            public bool CloseDataReader()
            {
                bool bResultat;
     
                bResultat = true;
     
                try
                {
                    if (m_SqlDataReader != null)
                    {
                        m_SqlDataReader.Close();
                    }
                    else
                    {
                        bResultat = false;
                    }
                }
                catch (System.Exception ex)
                {
                    bResultat = false;
                    throw ex;
                }
                return bResultat;
            }
     
            #endregion
     
            #region EXECUTION COMMAND
            /// <summary>
            /// retourne 1 valeur correspondant à la requéte
            /// </summary>
            /// <returns>un objet(peut étre un string,un int,etc.)</returns>
            public object ChargerValeur()
            {
                object oObjet;
     
                try
                {
                    if (m_SqlConnection.State == System.Data.ConnectionState.Closed)
                    {
                        m_SqlConnection.Open();
                    }
                    oObjet = m_SqlCommand.ExecuteScalar();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                return oObjet;
            }
            /// <summary>
            /// retourne une liste(table)
            /// </summary>
            /// <returns>un DataReader</returns>
            public System.Data.SqlClient.SqlDataReader LoadList()
            {
                try
                {
                    if (m_SqlConnection.State == System.Data.ConnectionState.Closed)
                    {
                        m_SqlConnection.Open();
                    }
                    m_SqlDataReader = m_SqlCommand.ExecuteReader();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
     
                return m_SqlDataReader;
            }
            /// <summary>
            /// permet ajouter(insert),modifier(update),supprimer(delete),etc.
            /// </summary>
            /// <returns>le nombre de lignes mises à jour</returns>
            public int Save()
            {
                int nLignes;
                try
                {
                    nLignes = 0;
     
                    if (m_SqlConnection.State == System.Data.ConnectionState.Closed)
                    {
                        m_SqlConnection.Open();
                    }
                    nLignes = m_SqlCommand.ExecuteNonQuery();
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw ex;
                }
                catch (System.InvalidOperationException exs)
                {
                    throw exs;
                }
                return nLignes;
            }
     
            #endregion
     
            #endregion
        }
    }
    qui existe au niveau BDATA (bibliothque de classe 1), et au niveau de l'application windows j'ai mis mon fichier app.config j'ai ajouté la ref configuration et j'ai verifié le .exe du .config tout est en bon etat mais je recupére toujours null au niveau de ma chaine de connexion ?

  7. #7
    Expert éminent sénior

    Avatar de Philippe Vialatte
    Homme Profil pro
    Architecte technique
    Inscrit en
    Juillet 2004
    Messages
    3 029
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juillet 2004
    Messages : 3 029
    Points : 12 465
    Points
    12 465
    Par défaut
    le fichier de configuration pris en compte est celui qui corresponds a ton exe...

    Si tu regardes dans ton .config du .exe, est-ce que tu as "conString" ? Si non, ca ne fonctionnera pas (ou alors, j'ai pas tout compris )

    Mon Blog

    The Cake is still a lie !!!



    Vous voulez contribuer à la rubrique .NET ? Contactez-moi par MP.
    Vous voulez rédiger des articles pour la rubrique .NET ? Voici la procédure à suivre.

  8. #8
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut
    effectivement le conString existe au niveau app.config dans l'application win (le .exe existe) , le probleme c'est que quand j'ai testé l'application chez autre collègues, j'ai pu récupéré la chaine !

  9. #9
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut
    la petite application que j'ai mis pour tester ma chaine de connexion ne marche plus, je l'ai testé hier ca marche mais quand j'ai voulu revérifier j'ai constaté que ma chaine est null

Discussions similaires

  1. [Axis]Probleme avec server-config.wsdd
    Par Rtitoun dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 15/07/2008, 14h40
  2. [VB.NET] Recuperer un paramettre avec App.Config
    Par Monster77 dans le forum Windows Forms
    Réponses: 21
    Dernier message: 31/12/2007, 14h06
  3. [OpenSuse 10.2] problem avec ntfs-config
    Par wodel dans le forum Applications et environnements graphiques
    Réponses: 0
    Dernier message: 17/11/2007, 13h04
  4. Liaison tardive avec app.config
    Par Promeneur dans le forum C#
    Réponses: 4
    Dernier message: 16/03/2007, 12h29
  5. [Struts] Problème avec struts-config.xml
    Par The_freeman dans le forum Struts 1
    Réponses: 6
    Dernier message: 29/01/2006, 22h55

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