我不知道發生了什麼事。此代碼昨天工作正常,但今天當我嘗試運行它時,我在標記的位置收到「指定的轉換無效」錯誤。該查詢返回1 &之間的整數25.在Access數據庫中,該字段的數據類型爲'數字',字段大小='長整數'。無效,但它曾經是
任何線索如何糾正?
static void PopulateClientList()
{
Console.WriteLine("Populating Client List...");
Console.WriteLine("\r");
OleDbConnection conn = new OleDbConnection(strAccessConnABS);
string query = "SELECT DISTINCT Client FROM ORDERS WHERE Status = 'On Hold';";
OleDbCommand command = new OleDbCommand(query, conn);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
//OleDbDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
try
{
//if (reader.HasRows)
//{
// while (reader.Read())
// {
//clientlist.Add(reader.GetInt32(0)); //cast not valid error happens here
//}
// }
adapter.Fill(dt);
if (dt.Rows.Count >= 1)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][0].ToString() != "")
clientlist.Add((int)dt.Rows[i][0]);
}
}
}
catch (OleDbException ex)
{
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
你在代碼中改變了什麼?你在數據庫中改變了什麼? – meilke
拋出異常時reader [0]的值是什麼? – Jonesopolis
Convert.ToInt32(reader.GetValue(0))? – Krishna