jeudi 21 janvier 2016

System.Data.SQLite throws IndexOutOfRangeException when reading

I'm currently programming a bot for Twitch, where we want to store settings in a database.

The database itself is pre-created, so all columns which are requested, are there.

Somehow, when using the reader object, it immediately throws an IndexOutOfRangeException. I don't know what causes this error.

public static void GetConfigFromDb()
    {
        try
        {
            SQLiteConnection conn = new SQLiteConnection("Data Source=OakSettings.sqlite;");
            conn.Open();
            string sql = "SELECT * FROM oak_settings";
            SQLiteCommand command = new SQLiteCommand(sql, conn);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                //StreamerOAuthKey = (string)reader["StreamerOAuthToken"];
                //BotOAuthKey = (string)reader["BotOAuthToken"];
                //StreamerUsername = (string)reader["StreamerTwitchUsername"];
                //BotUsername = (string)reader["BotTwitchUsername"];
                StreamerOAuthKey = reader.GetString(reader.GetOrdinal("StreamerOAuthToken"));
                BotOAuthKey = reader.GetString(reader.GetOrdinal("BotOAuthToken"));
                StreamerUsername = reader.GetString(reader.GetOrdinal("StreamerTwitchUsername"));
                BotUsername = reader.GetString(reader.GetOrdinal("BotTwitchUsername"));
            }
            conn.Close();
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.ToString());
        }
    }

The only occasion when this method gets called, is on startup of the application. The part which is commented, was the way I did it before, but then I changed it, in hope this was causing the error.

What causes the error?

Aucun commentaire:

Enregistrer un commentaire