vendredi 30 janvier 2015

Why do I get "InvalidOperationException: No current row" with this code?

I'm trying to retrieve a row from a table using the GetInventoryRecordForUPC() method below. I can retrieve the row fine in LINQPad by entering "SELECT invName, line_id, ref_no, upc_code, description, department, vendor_id, upc_pack_size, pack_size, id, unit_cost, unit_list, unit_qty, new_item FROM Inventory WHERE upc_code = '76145513'" (the value passed in "upc" is "76145513") in my test scenario.


But when the method below runs, the app crashes, and the log file contains:



System.InvalidOperationException: No current row
at System.Data.SQLite.SQLiteDataReader.CheckValidRow()
at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
at System.Data.SQLite.SQLiteDataReader.GetString(Int32 i)
at HHS.TestHHSDBUtils.GetInventoryRecordForUPC(String upc)


The method has the table's create statement commented out to show the names and data types of the columns.



public Inventory GetInventoryRecordForUPC(String upc)
{
ExceptionLoggingService.Instance.WriteLog("Reached
TestHHSDBUtils.GetInventoryRecordForUPC");
Inventory inv = new Inventory();
using (SQLiteConnection conn = new
SQLiteConnection(HHSUtils.GetDBConnection()))
{
conn.Open();
const string qry = "SELECT invName, line_id, ref_no, upc_code,
description, department, vendor_id, upc_pack_size, pack_size, id, unit_cost,
unit_list, unit_qty, new_item FROM Inventory WHERE upc_code = @UPCCode";
//CREATE TABLE Inventory(invName TEXT, line_id INTEGER, ref_no
TEXT, upc_code TEXT, description TEXT, department REAL, vendor_id TEXT,
upc_pack_size INTEGER, pack_size INTEGER, id TEXT, unit_cost REAL, unit_list
REAL, unit_qty REAL, new_item INTEGER, siteNum TEXT, Created TEXT, Modified
TEXT)";

using (SQLiteCommand cmd = new SQLiteCommand(qry, conn))
{
cmd.Parameters.Add(new SQLiteParameter("UPCCode", upc));
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
inv.invName = rdr.GetString(0);
inv.line_id = rdr.GetInt32(1);
inv.ref_no = rdr.GetString(2);
inv.upc_code = rdr.GetString(3);
inv.description = rdr.GetString(4);
inv.department = rdr.GetFloat(5);
inv.vendor_id = rdr.GetString(6);
inv.upc_pack_size = rdr.GetInt32(7);
inv.pack_size = rdr.GetInt32(8);
inv.id = rdr.GetString(9);
inv.unit_cost = rdr.GetFloat(10);
inv.unit_list = rdr.GetFloat(11);
inv.unit_qty = rdr.GetFloat(12);
inv.new_item = rdr.GetInt32(13);
}
}
return inv;
}
}


The Inventory class is declared this way:



public class Inventory
{
public String invName { get; set; }
public int line_id { get; set; }
public String ref_no { get; set; }
public String upc_code { get; set; }
public String description { get; set; }
public double department { get; set; }
public String vendor_id { get; set; }
public int upc_pack_size { get; set; }
public int pack_size { get; set; }
public String id { get; set; } // id, here?
public double unit_cost { get; set; } // REAL in SQLite
public double unit_list { get; set; } // REAL in SQLite
public double unit_qty { get; set; } // REAL in SQLite
public int new_item { get; set; }
// Create Table code adds TEXT siteNum, Created and Modified columns
}


Why in John Steinbeck's pet poodle would this cause a "InvalidOperationException: No current row"?!?


Aucun commentaire:

Enregistrer un commentaire