## 3 replies

Last post Sep 12, 2018 03:23 AM by Ackerly Xu

• JagjitSingh

Member

504 Points

1776 Posts

### Null Value

Hi

In below statements i want to check for Null Value . In case of integer or decimal if it is null then 0 , in case of string if null then ""

string m_Location0 = dt.Rows[i]["Location"].ToString();
decimal m_Amount = Convert.ToDecimal(dt.Rows[i]["Amount"].ToString());

Thanks

• msdevm

Member

56 Points

173 Posts

### Re: Null Value

Try this quick and dirty

decimal m_Amount = CTODecimal(dt.Rows[i]["Amount"].ToString());

public Decimal CTODecimal(string pParamValue)
{
Decimal nReturnValue = 0
try
{
nReturnValue = Convert.ToDecimal(pParamValue)
}
catch (Exemption e)
{
Console.Write(e.Message.ToString());
}

Good Luck

• oned_gk

All-Star

52443 Points

15640 Posts

### Re: Null Value

string m_Location0 = dt.Rows[i]["Location"] != DBNull.Value ? dt.Rows[i]["Location"].ToString() : "";
decimal m_Amount = dt.Rows[i]["Amount"] != DBNull.Value ? Convert.ToDecimal(dt.Rows[i]["Amount"]) : 0;

or simply modify select command that similar above code

Select isnull(location,'') as location, isnull(amount,0) as amount from yourtable

So, don't worry about null value

Programming to simplify, don't look for difficult way
• Ackerly Xu

Contributor

3480 Points

1300 Posts

### Re: Null Value

Sep 12, 2018 03:23 AM|Ackerly Xu|LINK

Hi JagjitSingh,

You could try code below. Just check using DbNull.Value.

string m_Location;
decimal m_Amount;
for (int i = 0; i < table.Rows.Count; i++)
{
if(table.Rows[i]["Location"] == DBNull.Value)
{
m_Location = "";

}
else
{
m_Location = table.Rows[i]["Location"] as string;
}
if (table.Rows[i]["Amount"] == DBNull.Value)
{
m_Amount = 0;
}
else
{
m_Amount = Convert.ToDecimal(table.Rows[i]["Amount"].ToString());

}

}

Best regards,

Ackerly Xu

MSDN Community Support