asp.net - System.IndexOutOfRangeException on SQLDataReader Value Using C# -
i have sqldatareader returns 3 integers. however, there occasions when 2 of integers return null values.
to round wrote following:
int shoppingcartheadid = 0; int billid = 0; int delid = 0; conn.open(); reader = comm.executereader(); if (reader.read()) { shoppingcartheadid = convert.toint32(reader["shoppingcartheadid"]); if (!reader.isdbnull(billid)) { billid = convert.toint32(reader["billid"]); } if (!reader.isdbnull(delid)) { delid = convert.toint32(reader["delid"]); } } reader.close();
unfortunately i'm still getting error message. suggestions?
ps tried no luck
if (reader["billid"] != null)
i try access index instead of column name, in case passing not existing column name.
also, make sure wrap reader using block in case if there exception reader closed , disposed, example in way:
... using(var reader = comm.executereader()) { if (reader.read()) { shoppingcartheadid = convert.toint32(reader[0]); if (!reader.isdbnull(1)) { billid = convert.toint32(reader[1]); } if (!reader.isdbnull(2)) { delid = convert.toint32(reader[2]); } } }
Comments
Post a Comment