# Null Value

• ### Question

• User-1499457942 posted

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

Tuesday, September 11, 2018 2:54 PM

• User-1716253493 posted
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

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, September 12, 2018 3:01 AM

### All replies

• User1266736465 posted

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

Wednesday, September 12, 2018 12:19 AM
• User-1716253493 posted
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

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, September 12, 2018 3:01 AM
• User-893317190 posted

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

Wednesday, September 12, 2018 3:23 AM