Getting an error while trying to set index using a variable


New member
Sep 12, 2018
Programming Experience
Dear all
I have following function:
        public ArrayList GetAllRows(string tablename)
            // Set database object
            SQLiteConnection db = new SQLiteConnection(this.constr);
            // Execute SQL
            string sql = $"SELECT * FROM {tablename};";
            SQLiteCommand cmd = new SQLiteCommand(sql, db);
            // Go through received data
            SQLiteDataReader reader = cmd.ExecuteReader();
            // Add list to contain rows
            ArrayList rows = new ArrayList();
            int row_count = 0; // set integer to count the rows of the selected query
            while (reader.Read() == true)
                row_count = row_count + 1;
            List<string> row = new List<string>();
            while (reader.Read() == true)
                foreach(string colname in this.GetColumns(tablename))
                    string col = Convert.ToString(colname);
            return rows;

But I get an error, which is as follows:
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'object' to 'string' DbActions C:\Users\jordy\source\repos\sqlexplo\DbActions\DbAction.cs 84 Active

Is there somebody here who can give me a tip on where to search the mistake and maybe a hint about how to solve it? I don't find the issue and it doesn't help that I don't have an overview of the code- since I am blind.

Thanks in advance for the possible replies.

Regards, Jordy
Firstly, the error message tells you exactly what line the error occurs on and yet you have left us to work that out for ourselves. Please provide ALL the relevant information. We shouldn't have to assume, guess or work out anything that you already know.

The issue is presumably here:

That's because the indexer for a data reader is type Object, because it needs to be able to return any type of object. You're adding the data to a List<string> so you presumably know that the data is a String. In that case you should either cast the data as type String:
row.Add((string) reader[col]);

or use the GetString method that the data reader provides for getting String data. Just note that that method only takes a column index and not a column name, so you need to get the index for the name you have first:

Now to two points unrelated to your issue. Firstly, don't use an ArrayList for anything. You're already using a List<T> there so you know about them, so why use an ArrayList elsewhere? If you want to store List<string> objects in a list then use a List<List<string>>.

That said, I don't think that you should be using a List<string> in the first place. It would be better to define a class that has a property for each value you want to store and create instances of that type, then store those objects in a List<T> where T is the type you defined.

Finally, what's up with this:
foreach([B][U][COLOR="#FF0000"]string colname[/COLOR][/U][/B] in this.GetColumns(tablename))
    string col = [B][U][COLOR="#FF0000"]Convert.ToString(colname)[/COLOR][/U][/B];
Why exactly do you think you need to convert a String to a String?
Top Bottom