Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is here..

C#
foreach (DataRow dr in gv1.Rows)
{
    decimal Total_Sqrft = Convert.ToDecimal(
        String.IsNullOrEmpty(Convert.ToString(dr[1])) ?? "0" : Convert.ToString(dr[1])
    );
}


Updated:
I solved It Using..

C#
if(String.IsNullOrEmpty(Convert.ToString(dr[1])))
{
    decimal Total_Sqrft=0;
}
else
{
    decimal Total_Sqrft = Convert.ToDecimal(dr[1]);
}

But I want to know about the above operation.
Posted
Updated 11-Sep-14 21:41pm
v3
Comments
george4986 12-Sep-14 3:36am    
if dr[1] is null or empty Total_Sqrft is set to zero.
if any numeric value is passed it will be converted to decimal.
if any character or special sharacters are passed it will throw error .

The first thing you need to check is if dr[1] is not null (assuming there are at least two columns in the data table).
Then you can use the method dr[1].ToString() to convert to a string.

[Updated: Also takes care of empty string]
The "long winded" way for easy reading:
C#
decimal Total_Sqrft = 0;
foreach (DataRow dr in gv1.Rows)
{
    Total_Sqrft = 0;
    if (dr[1] != null)
    {
        string tmp = dr[1].ToString();
        if (tmp != String.Empty)
            Total_Sqrft = decimal.Parse(tmp);
    }
}


The one-liner
C#
decimal Total_Sqrft = 0;
foreach (DataRow dr in gv1.Rows)
{
    // The below code can be read:
    // if dr[1] is not null, use the value in the cell otherwise default to 0.
    Total_Sqrft = decimal.Parse((dr[1] == null || dr[1].ToString() == String.Empty) ? "0" : dr[1].ToString());
}


You can also look into decimal.TryParse(...)
 
Share this answer
 
v3
Comments
/\jmot 12-Sep-14 5:19am    
i use ..
Total_Sqrft =Convert.ToDecimal((String.IsNullOrEmpty(Convert.ToString(dr[1]))) ? "0" : Convert.ToString(dr[1]));

similar to your solution..
thank you. :)
George Jonsson 12-Sep-14 5:28am    
You need to look again.
In your case you will get an exception here Convert.ToString(dr[1]) if dr[1] is null.
I also updated my solution to take care of the empty string case.
/\jmot 12-Sep-14 5:34am    
Total_Sqrft =Convert.ToDecimal((String.IsNullOrEmpty(dr[1])) ? "0" : Convert.ToString(dr[1]));
now??
George Jonsson 12-Sep-14 5:40am    
Does it work when you test your code?
Test cases:
dr[1] = null;
dr[1] = "";
dr[1] = 10;
dr[1] = "xy";
I have given you a solution and if you don't like it, what do you want me to do?
George Jonsson 12-Sep-14 5:45am    
String.IsNullOrEmpty() only works if the column represented by dr[1] is of type System.String.
You have given no information about that.
Please, don't write code like that - you have to stop and look at it pretty closely in order to work out what is going on. Write it long hand if it's going to be like that.

But...what the heck is that colon doing in there?
The null coalescence operator "??" only takes two parameters - one on the left and one on the right.
Are you confusing this with the conditional operator "?:" ?
 
Share this answer
 
Add a custom Function for the conversion

C#
public decimal CnvToDecimal(object Data)
        {
            try
            {
                return System.Convert.ToDecimal(Data);
            }
            catch
            {
                return 0;
            }
        }


and call the function like this

C#
foreach (DataRow dr in gv1.Rows)
           {
               decimal Total_Sqrft = CnvToDecimal(dr[1]);
           }


good luck ;-)
 
Share this answer
 
Comments
George Jonsson 12-Sep-14 4:31am    
Never use try-catch when you easily can predict the result.
It just slows things down.
Also in this case it hides problems with the format in the cell.
This might be desired, but maybe not.
george4986 12-Sep-14 4:50am    
i have seen ur solution ,it will throw exception if a string value is passed.
why didn't u predict that ?
George Jonsson 12-Sep-14 5:02am    
If you by string value mean letters, I have not taken care of that.
That is intentional because it might be a bad idea to hide this problem.
Why would it be ok for a cell in a data table that is supposed to be converted to a number, to contain something that is not a number.
This depends on the requirements of the application, which I know nothing about.
I have spent many hours to find out why something didn't work without any proper error indication due to improper use of try-catch.
george4986 12-Sep-14 5:20am    
you should taken care of that also coz its an error.
converted to decimal becoz it is going to be placed in a container of type decimal.
George Jonsson 12-Sep-14 5:31am    
Not sure I understand your point.
If dr[1] contains the value "aa" an error with the message "Input string was not in a correct format." will be thrown.
So how should that error be handled?
I don't know coz I have not seen the requirements, so better to make sure that the error is exposed than to hide it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900