Question Cannot index type 'object'?

elais12

Member
Joined
Jul 20, 2018
Messages
7
Programming Experience
Beginner
Hello together

I developed a little Windows App with 2 Listfields (named LstAnzeige and LstAnzeige1) and SQL Database in the Background.

Now i want to delete the Database Entry if the User clicks on the List Entry but the following error appears and the program won't be built:

"Cannot apply indexing with [] to an expression of type 'object' "

Below the Snippet of the "Delete" Button
C#:
private void DBDelete_Click(object sender, EventArgs e)
        {
            // DB Zugriff und Daten löschen mit Delete
            OleDbConnection con = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();

            con.ConnectionString = "Provider=SQLNCLI11.1;" + "Data Source=CRONOS-WI764BIT;Initial Catalog=Tageslimit;Persist Security Info=True;User ID=sa;Password=*****;
            cmd.Connection = con;
            // Prüfen ob im Listenefeld der Nettolohn angeklickt wurde
            if (LstAnzeige.Text == "")
            {
                // falls ja, Meldung an User dass er einen Datensatz anklicken soll
                MessageBox.Show("Bitte einen Datensatz auswählen");
                return;
            }
            // Warnung vor DB Delete ausgeben
            if (MessageBox.Show("Wollen Sie den ausgewählten " +
                    "Datensatz wirklich löschen?", "Löschen",
                    MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            try
            {
                // Falls der User Ja geklickt hat, aktiven Datensatz aus DB löschen
                con.Open();
                cmd.CommandText = "DELETE FROM Limiten WHERE Nettoloh" + "Nettolohn = " + Nettolohn[LstAnzeige.SelectedIndex]; "
                 
                   
                MessageBox.Show(cmd.CommandText);

                int anzahl = cmd.ExecuteNonQuery();
                if (anzahl > 0)
                    MessageBox.Show("Datensatz gelöscht");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            con.Close();
        }
 
In future, please indicate exactly where in the code the error occurs. We shouldn't have to spend time working things out that you already know. I'm assuming that the issue occurs here:
cmd.CommandText = "DELETE FROM Limiten WHERE Nettoloh" + "Nettolohn = " + Nettolohn[LstAnzeige.SelectedIndex]; "

That's the only place I see you indexing anything. There are a number of issues there. Firstly, there appears to be a stray double-quote at the end of the line. Secondly, if that did build then I very much doubt that the string it produced would contain what you intended. Maybe you should have closer look at that.

As for the issue you mention, you don't show us what Nettolohn is or where you set it so we can only guess but the error message is telling you that you can't index something of type 'object' so it is presumably declared as type object. Why is that? What do you think it should be and, if it's something other than object, why haven't you declared it as that type?
 
Back
Top Bottom