I am getting compile-time error at reader.GetString, any idea why?
Code:
using (var connection = new OleDbConnection())
{
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Sparrow vivek\Documents\Billing.accdb";
connection.Open();
var query = "SELECT ItemCode FROM invoice";
using (var command = new OleDbCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
comboBox1.Items.Add(reader.GetString("ItemCode"));
comboBox2.Items.Add(reader.GetString("ItemCode"));
}
}
}
}

OleDbDataReader.GetStringrequires an input ofint. It expects the column ordinal, not the column name.Either use the column ordinal directly, or determine the ordinal ahead of time. You can determine the column ordinal by using
OleDbDataReader.GetOrdinal:Since you are doing this in a loop, you could do something like this: