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 :

CS0118 Variable mais utilisé comme un type


Sujet :

C#

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2021
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2021
    Messages : 13
    Points : 6
    Points
    6
    Par défaut CS0118 Variable mais utilisé comme un type
    Bonsoir à vous !
    Je suis débutant en c# et en stage, j'ai eu comme activité de projet de mettre un tableau dans un DataGridView sur Visual Studio 2019.
    J'ai trouvé un petit code que j'ai modifié et adapté pour qu'il colle avec mes besoins mais un problème récurant est survenu et je n'y arrive pas à m'en débarrasser...

    J'ai une erreur de Visual me disant que ma variable cmd est utilisé comme un type et que cela ne marche pas (erreur CS0118 dans Visual).

    Voici mon bout de code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    private void btnCharger_Click(object sender, EventArgs e)
            {
                //string nomFichier ="*****.xlsx";
                //déclaration et utilisation d'un OLeDBConnection 
                    OleDbConnection Excel =
                    new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source =*****.xlsx; Extended Properties = ""Excel 12.0 Xml;HDR=YES"";");
                    Excel.Open();
                    DataTable tableDonnees = new DataTable();
                    //notre requête sélectionne toute les cellule de la Feuil1 
                    OleDbCommand cmd = new OleDbCommand("select * from [*****$]", Excel);
                    OleDbDataReader dr = new cmd.ExecuteReader();
                    tableDonnees.Load(dr);
                    dtGridDonnees.DataSource = tableDonnees;
            }
    Merci beaucoup à vous pour votre temps et votre aide !

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2021
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2021
    Messages : 13
    Points : 6
    Points
    6
    Par défaut
    Re-bonsoir !
    Alors j'ai réglé le problème, j'avais ajouté un new en trop au moment de l'instanciation de ma variable cmd dans la variable dr de la classe "OleDbDataReader".
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    OleDbDataReader dr = cmd.ExecuteReader(); //à ce niveau là
    Mais maintenant au niveau de mon formulaire (avec mes boutons et tout), mon bouton pour charger ma feuille Excel ne marche pas.
    Des idées ?

  3. #3
    Membre chevronné
    Homme Profil pro
    edi
    Inscrit en
    Juin 2007
    Messages
    898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : edi

    Informations forums :
    Inscription : Juin 2007
    Messages : 898
    Points : 1 915
    Points
    1 915
    Par défaut
    Je suppose que tu es en WinForms. Est-ce-que dans le code du designer tu as l'ajout du handler (la méthode btnCharger_Click) à l'événement clic du bouton : btnCharger.Click += btnCharger_Click; ?

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2021
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2021
    Messages : 13
    Points : 6
    Points
    6
    Par défaut
    Citation Envoyé par Noxen Voir le message
    Je suppose que tu es en WinForms. Est-ce-que dans le code du designer tu as l'ajout du handler (la méthode btnCharger_Click) à l'événement clic du bouton : btnCharger.Click += btnCharger_Click; ?
    Salut, merci pour ton aide, je suis bien sous WinForms pour programmer l'interface de ce projet et j'aurai bien voulu te répondre mais bizarrement comparer à mon "FrmAccueil.Designer.cs" je n'ai pratiquement pas de code :
    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
    protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.dtGridDonnees = new System.Windows.Forms.DataGridView();
                this.btnCharger = new System.Windows.Forms.Button();
                this.label2 = new System.Windows.Forms.Label();
                this.TxtRecherche = new System.Windows.Forms.TextBox();
                this.btnExit = new System.Windows.Forms.Button();
                this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                ((System.ComponentModel.ISupportInitialize)(this.dtGridDonnees)).BeginInit();
                this.SuspendLayout();
                // 
                // dtGridDonnees
                // 
                this.dtGridDonnees.AllowUserToAddRows = false;
                this.dtGridDonnees.AllowUserToDeleteRows = false;
                this.dtGridDonnees.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dtGridDonnees.Location = new System.Drawing.Point(40, 92);
                this.dtGridDonnees.Name = "dtGridDonnees";
                this.dtGridDonnees.RowHeadersWidth = 51;
                this.dtGridDonnees.RowTemplate.Height = 24;
                this.dtGridDonnees.Size = new System.Drawing.Size(1116, 440);
                this.dtGridDonnees.TabIndex = 0;
                // 
                // btnCharger
                // 
                this.btnCharger.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                this.btnCharger.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnCharger.Location = new System.Drawing.Point(829, 545);
                this.btnCharger.Name = "btnCharger";
                this.btnCharger.Size = new System.Drawing.Size(124, 51);
                this.btnCharger.TabIndex = 4;
                this.btnCharger.Text = "Charger";
                this.btnCharger.UseVisualStyleBackColor = true;
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Font = new System.Drawing.Font("Microsoft JhengHei", 13.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.label2.Location = new System.Drawing.Point(62, 42);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(144, 31);
                this.label2.TabIndex = 3;
                this.label2.Text = "Rechercher";
                // 
                // TxtRecherche
                // 
                this.TxtRecherche.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.TxtRecherche.Location = new System.Drawing.Point(221, 46);
                this.TxtRecherche.Name = "TxtRecherche";
                this.TxtRecherche.Size = new System.Drawing.Size(707, 27);
                this.TxtRecherche.TabIndex = 1;
                // 
                // btnExit
                // 
                this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Abort;
                this.btnExit.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnExit.Location = new System.Drawing.Point(1002, 545);
                this.btnExit.Name = "btnExit";
                this.btnExit.Size = new System.Drawing.Size(124, 51);
                this.btnExit.TabIndex = 5;
                this.btnExit.Text = "Quitter";
                this.btnExit.UseVisualStyleBackColor = true;
                // 
                // openFileDialog1
                // 
                this.openFileDialog1.FileName = "openFileDialog1";
                // 
                // FrmDonnees
                // 
                this.AcceptButton = this.btnCharger;
                this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(1202, 608);
                this.Controls.Add(this.btnExit);
                this.Controls.Add(this.TxtRecherche);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.btnCharger);
                this.Controls.Add(this.dtGridDonnees);
                this.Name = "FrmDonnees";
                this.Text = "Données";
                this.Load += new System.EventHandler(this.FrmDonnees_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dtGridDonnees)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();
            }
     
     
            private System.Windows.Forms.DataGridView dtGridDonnees;
            private System.Windows.Forms.Button btnCharger;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.TextBox TxtRecherche;
            private System.Windows.Forms.Button btnExit;
            private System.Windows.Forms.OpenFileDialog openFileDialog1;
        }
    Je n'ai que ça. Des solutions :( ?

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2021
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2021
    Messages : 13
    Points : 6
    Points
    6
    Par défaut
    Je viens de voir que le site vient de générer le code donc effectivement non, il n'y a rien dans le designer representant un événement comme "btnCharger_Click". Je vais voir si j'arrive à résoudre quelque chose à partir de ça. Merci !

  6. #6
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 674
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 674
    Points : 5 259
    Points
    5 259
    Par défaut
    Pour ajouter ton évènement click, tu peut le faire avec l'IHM.
    1. Clique sur ton bouton
    2. Dans la fenêtre des propriétés (clic-droit sur le bouton, puis "Propriétés" si la fenêtre n'est pas visible), cliquer sur l'éclair
    3. Dans la combo à coté de "Click" sélectionner ta méthode "btnCharger_Click"

    Sinon directement dans le code du fichier Designer.
    1. Rechercher la ligne this.btnCharger.Text = "Charger";
    2. A cet endroit ajouter la ligne this.btnCharger.Click += System.EventHandler(this.btnCharger_Click);

Discussions similaires

  1. [Debutant] methode, mais utiliser comme type
    Par tornade69 dans le forum C#
    Réponses: 2
    Dernier message: 18/05/2013, 20h49
  2. Réponses: 12
    Dernier message: 18/04/2011, 21h25
  3. Cgi utilisé comme Relais a spam :
    Par sam.fet dans le forum Général Conception Web
    Réponses: 3
    Dernier message: 17/06/2006, 11h44
  4. Port USB utilisé comme 1 port série
    Par Arnaud Malabeux dans le forum C++
    Réponses: 2
    Dernier message: 06/06/2006, 11h03
  5. tv lcd utilisé comme écran pc
    Par sweety107 dans le forum Périphériques
    Réponses: 4
    Dernier message: 07/11/2005, 08h32

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