Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

If you actually just need the empty string instead of null, and you don't care about alerting the user that the value was null, just do:

var name = (item["Name"] ?? "").ToString(); 
var dueDate= (item["Due Date"] ??).ToString();
...

Alternatively, you could even make a small helper method to do this for you:

public static string GetValue(object o)
{
 if (o == null) return "";
 else return o.ToString();
}

and call it as:

var name = GetValue(item["Name"]); 
var dueDate= GetValue(item["Due Date"]);

You won't get any exceptions with either of these ways - just empty strings. You should avoid throwing (or deliberately expecting) an exception whenever possible. See this answer this answer and some of the other answers on that page - exceptions should be for an unexpected scenario, not normal control flow.


In the case where the column doesn't even exist when it has no data (which indicates an issue with the generation of the data), you can modify it as follows:

public static string GetValue(DataRow row, string column)
{
 if (!row.Table.Columns.Contains(column) || row[column] == null) return "";
 else return row[column].ToString();
}

If you actually just need the empty string instead of null, and you don't care about alerting the user that the value was null, just do:

var name = (item["Name"] ?? "").ToString(); 
var dueDate= (item["Due Date"] ??).ToString();
...

Alternatively, you could even make a small helper method to do this for you:

public static string GetValue(object o)
{
 if (o == null) return "";
 else return o.ToString();
}

and call it as:

var name = GetValue(item["Name"]); 
var dueDate= GetValue(item["Due Date"]);

You won't get any exceptions with either of these ways - just empty strings. You should avoid throwing (or deliberately expecting) an exception whenever possible. See this answer and some of the other answers on that page - exceptions should be for an unexpected scenario, not normal control flow.


In the case where the column doesn't even exist when it has no data (which indicates an issue with the generation of the data), you can modify it as follows:

public static string GetValue(DataRow row, string column)
{
 if (!row.Table.Columns.Contains(column) || row[column] == null) return "";
 else return row[column].ToString();
}

If you actually just need the empty string instead of null, and you don't care about alerting the user that the value was null, just do:

var name = (item["Name"] ?? "").ToString(); 
var dueDate= (item["Due Date"] ??).ToString();
...

Alternatively, you could even make a small helper method to do this for you:

public static string GetValue(object o)
{
 if (o == null) return "";
 else return o.ToString();
}

and call it as:

var name = GetValue(item["Name"]); 
var dueDate= GetValue(item["Due Date"]);

You won't get any exceptions with either of these ways - just empty strings. You should avoid throwing (or deliberately expecting) an exception whenever possible. See this answer and some of the other answers on that page - exceptions should be for an unexpected scenario, not normal control flow.


In the case where the column doesn't even exist when it has no data (which indicates an issue with the generation of the data), you can modify it as follows:

public static string GetValue(DataRow row, string column)
{
 if (!row.Table.Columns.Contains(column) || row[column] == null) return "";
 else return row[column].ToString();
}
added 387 characters in body
Source Link
Bobson
  • 1k
  • 5
  • 13

If you actually just need the empty string instead of null, and you don't care about alerting the user that the value was null, just do:

var name = (item["Name"] ?? "").ToString(); 
var dueDate= (item["Due Date"] ??).ToString();
...

Alternatively, you could even make a small helper method to do this for you:

public static string GetValue(object o)
{
 if (o == null) return "";
 else return o.ToString();
}

and call it as:

var name = GetValue(item["Name"]); 
var dueDate= GetValue(item["Due Date"]);

You won't get any exceptions with either of these ways - just empty strings. You should avoid throwing (or deliberately expecting) an exception whenever possible. See this answer and some of the other answers on that page - exceptions should be for an unexpected scenario, not normal control flow.


In the case where the column doesn't even exist when it has no data (which indicates an issue with the generation of the data), you can modify it as follows:

public static string GetValue(DataRow row, string column)
{
 if (!row.Table.Columns.Contains(column) || row[column] == null) return "";
 else return row[column].ToString();
}

If you actually just need the empty string instead of null, and you don't care about alerting the user that the value was null, just do:

var name = (item["Name"] ?? "").ToString(); 
var dueDate= (item["Due Date"] ??).ToString();
...

Alternatively, you could even make a small helper method to do this for you:

public static string GetValue(object o)
{
 if (o == null) return "";
 else return o.ToString();
}

and call it as:

var name = GetValue(item["Name"]); 
var dueDate= GetValue(item["Due Date"]);

You won't get any exceptions with either of these ways - just empty strings. You should avoid throwing (or deliberately expecting) an exception whenever possible. See this answer and some of the other answers on that page - exceptions should be for an unexpected scenario, not normal control flow.

If you actually just need the empty string instead of null, and you don't care about alerting the user that the value was null, just do:

var name = (item["Name"] ?? "").ToString(); 
var dueDate= (item["Due Date"] ??).ToString();
...

Alternatively, you could even make a small helper method to do this for you:

public static string GetValue(object o)
{
 if (o == null) return "";
 else return o.ToString();
}

and call it as:

var name = GetValue(item["Name"]); 
var dueDate= GetValue(item["Due Date"]);

You won't get any exceptions with either of these ways - just empty strings. You should avoid throwing (or deliberately expecting) an exception whenever possible. See this answer and some of the other answers on that page - exceptions should be for an unexpected scenario, not normal control flow.


In the case where the column doesn't even exist when it has no data (which indicates an issue with the generation of the data), you can modify it as follows:

public static string GetValue(DataRow row, string column)
{
 if (!row.Table.Columns.Contains(column) || row[column] == null) return "";
 else return row[column].ToString();
}
Source Link
Bobson
  • 1k
  • 5
  • 13

If you actually just need the empty string instead of null, and you don't care about alerting the user that the value was null, just do:

var name = (item["Name"] ?? "").ToString(); 
var dueDate= (item["Due Date"] ??).ToString();
...

Alternatively, you could even make a small helper method to do this for you:

public static string GetValue(object o)
{
 if (o == null) return "";
 else return o.ToString();
}

and call it as:

var name = GetValue(item["Name"]); 
var dueDate= GetValue(item["Due Date"]);

You won't get any exceptions with either of these ways - just empty strings. You should avoid throwing (or deliberately expecting) an exception whenever possible. See this answer and some of the other answers on that page - exceptions should be for an unexpected scenario, not normal control flow.

lang-cs

AltStyle によって変換されたページ (->オリジナル) /