If you know that it's an int
you should just cast it accordingly, that's the safest and most efficient approach:
int questionId = item.Field<int>("QuestionId"); // similar to (int) item["QuestionId"]
The Field
method also supports nullable types, so if it could be null:
int? questionId = item.Field<int?>("QuestionId");
if(questionId.HasValue) Console.Write(questionId.Value);
If it's actually a string
(why?) you have to cast it to string
and use int.Parse
:
int questionId = int.Parse(item.Field<string>("QuestionId"));
If you really don't know what it's type is, you can use System.Convert.ToInt32
:
int questionId = System.Convert.ToInt32(item["QuestionId"]);