3

All, I am trying to find a elegant way to retrieve value from a DataRow. Below is the way I had tried. please review it .And I think it is not good enough. Because it would be verbose when the number of fields which I want to retrieve is big. Any better idea? thanks.

  int result = -1;

  if (row["intCol"] != null)
  {
      bool bresult = int.TryParse(row["intCol"].ToString(), out result);
  }

Updated

The intCol data type in DB is nullable string.

Updated

Found it from Most efficient way to check for DBNull and then assign to a variable?

Cheers.

Community
  • 1
  • 1
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
  • 1
    if `row["intCol"]` is always an int, you can just do: `int result = (int)row["intCol"]`. other wise, check the type first. int->string->int conversion (what i think you're doing) seems wrong – x4rf41 Jun 10 '13 at 07:54
  • If you know what type is supposed to be there - `row.Field("intCol")` (to avoid failure with `DBNull`) – sq33G Jun 10 '13 at 07:54
  • Hi Sorry for forgetting to specify the DB type of `intCol`. It is nullable string. thanks. – Joe.wang Jun 10 '13 at 08:15

3 Answers3

3

Use the Field, IsNull, SetField methods to avoid dealing with DBNull.Value.

In the example you posted, you probably only need Field:

int? result = row.Field<int?>("intCol");
// if intCol is DBNull.Value, result will now be null.
bernhof
  • 6,219
  • 2
  • 45
  • 71
  • Beware that the `Field` method will simply cast the value to the specified type. It will not attempt a data type conversion. This means that if the value is actually a `decimal` and you've specified `int`, you'll get an `InvalidCastException`. If you're not entirely certain of the data type at runtime, you should stick to `Convert.ToInt32(row["intCol"])` to allow type conversion. – bernhof Jun 10 '13 at 08:36
  • ... combined with an `IsNull` check on the column beforehand. – bernhof Jun 10 '13 at 08:42
  • I see. so why don't use the `tryparse` especially when the original datatype is string . thanks. – Joe.wang Jun 10 '13 at 11:35
  • If the original type is a string I would definitely use `TryParse`. `Convert.ToXXX` *can* convert strings for you, but it always uses the user's current regional settings to determine how to parse numbers and dates. – bernhof Jun 10 '13 at 12:57
  • Another obvious benefit of using `TryParse` is the fact that it won't fail if string cannot be parsed. `Convert.ToXXX` will throw an exception. – bernhof Jun 10 '13 at 13:04
  • yes. right . It is the main purpose of the `TryParse`. I found We can use Invariant Culture when trying to parse the number and date. http://forums.asp.net/t/1592165.aspx/1 – Joe.wang Jun 10 '13 at 13:10
  • 1
    Exactly. Also see: http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32 – bernhof Jun 10 '13 at 13:13
1

You can use the built-in Field<int?> property to get the result:

var result = row.Field<int?>("intCol");
cuongle
  • 74,024
  • 28
  • 151
  • 206
1

You could use Convert (especially if the type might be also be BIGINT, SMALINT, ..).

 var i = Convert.ToInt32(row["intCol"]); // i is a int

Beware that would convert NULL into 0.

If you're pretty sure the DB Type is INT (and not BIGINT, SMALINT, ..), use "as" (DB NULL would become null reference)

 var i = row["intCol"] as int?; // i is a nullable int
Serge
  • 6,554
  • 5
  • 30
  • 56