c# - Parsing int from DataRow cell -
how int value parsed datarow cell?
int32.parse(item["questionid"].tostring());
this code works, looks verbose. possible handle dbnull values?
if know it's int
should cast accordingly, that's safest , efficient approach:
int questionid = item.field<int>("questionid"); // similar (int) item["questionid"]
the field
method supports nullable types, if null:
int? questionid = item.field<int?>("questionid"); if(questionid.hasvalue) console.write(questionid.value);
if it's string
(why?) have cast string
, use int.parse
:
int questionid = int.parse(item.field<string>("questionid"));
if don't know it's type is, can use system.convert.toint32
:
int questionid = system.convert.toint32(item["questionid"]);
Comments
Post a Comment