venkat
venkat

Reputation: 5738

How to check an object is Empty in C#.NET 3.5?

Continuation with the earlier post: How to check object is null or empty in C#.NET 3.5?

In my code i handled successfully if the object is null

BUT

I'm not getting How to check the DataRow object dtTable.Rows[0][4] is Empty or NOT

dblRevenue = Convert.ToDouble(dtTable.Rows[0][4]);

Please help!!

Upvotes: 1

Views: 621

Answers (4)

Gimhan
Gimhan

Reputation: 62

Try this solution. You can easily convert value without any exception.

long lValue = 0;
long.TryParse(dtTable.Rows[0][4], out lValue);

Upvotes: 0

Lyle
Lyle

Reputation: 11

if (dtTable.Rows[0][4] != null && dtTable.Rows[0][4] != DBNull.Value)
{
    dblRevenue = Convert.ToDouble(dtTable.Rows[0][4]);
    ...
}
else
{
    dblRevenue = 0.0;
}

May be it is so Bloated .

Upvotes: 1

McZ
McZ

Reputation: 1

You have to check, if the value of the cell is typed as System.DBNull. If it is, you cannot convert it to double without getting an exception on type conversion.

You do know that you can evaluate the type of a given item within the IDE?

Upvotes: 0

codeandcloud
codeandcloud

Reputation: 55248

Try this.

dblRevenue = dtTable.Rows[0][4] == DBNull.Value ? 0.00 : Convert.ToDouble(dtTable.Rows[0][4]);

Upvotes: 5

Related Questions