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

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -