Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am converting string to null value but it returns 0 but i want null value instead of 0.

my code is


C#
public decimal? HwareCost { get; set; }

decimal tmpHwareCost = 0.0m;
if (Decimal.TryParse(Convert.ToString((dataRow["HwareCost"])), out tmpHwareCost))
itAreaList.HwareCost = tmpHwareCost;


plz help me
Posted
Updated 16-Dec-14 19:52pm
v3

There are few things in your code to notice.
C#
1)Convert.ToString(dataRow["HwareCost"])

It will convert the object and will return the string and if object is null then it returns a blank/empty string.

C#
2)Decimal.TryParse

It will try to convert the object into decimal and if object is null then it will assign 0 to out variable.

If you want to have a null object into your decimal property then do the code like this.

C#
if(!Convert.IsDBNull(dataRow["HwareCost"]))
{
   itAreaList.HwareCost = Convert.ToDecimal(dataRow["HwareCost"]);
}
else
{
   itAreaList.HwareCost =  null;
}
 
Share this answer
 
v2
Comments
Maciej Los 17-Dec-14 2:20am    
Using IsDbNull or DbNull.Value is very good practice.
+5!
Praveen Kumar Upadhyay 17-Dec-14 2:32am    
Thank you Maciej.
DamithSL 17-Dec-14 2:44am    
Maciej, we can use Field extension method and get the value directly without all these validations
Maciej Los 17-Dec-14 3:08am    
Good point!
Praveen Kumar Upadhyay 17-Dec-14 3:12am    
Even I am agree with your point.
DataRow Field extension method as below[^]
C#
HwareCost = dataRow.Field<decimal?>("HwareCost");


LINQPAD sample code:
C#
decimal? HwareCost1 { get; set; }
decimal? HwareCost2 { get; set; }
void Main()
{
	var table = GetTable();
        //read first row value 
	HwareCost1 = table.Rows[0].Field<decimal?>("HwareCost") ;
	//HwareCost1.Dump(); //25.44
        //read 2nd row value 
	HwareCost2 = table.Rows[1].Field<decimal?>("HwareCost") ;
	//HwareCost2.Dump(); //null
}

static DataTable GetTable()
{
	DataTable table = new DataTable();
	DataColumn column;
	column = new DataColumn("HwareCost", 
	System.Type.GetType("System.Decimal"));
	column.AllowDBNull = true;
	table.Columns.Add(column);
	table.Columns.Add("Date", typeof(DateTime));
	table.Rows.Add(25.44m,DateTime.Now);
	table.Rows.Add(null,DateTime.Now);
	return table;
}
 
Share this answer
 
v3
Comments
Praveen Kumar Upadhyay 17-Dec-14 2:23am    
This is very nice, but this won't give exception if "HwareCost" has a null object?
DamithSL 17-Dec-14 2:29am    
why you need exception? you can check HwareCost null or not. there is no need of exception.
Praveen Kumar Upadhyay 17-Dec-14 2:30am    
So it will require a check, otherwise it will give exception. right?
DamithSL 17-Dec-14 2:37am    
No
DamithSL 17-Dec-14 2:44am    
I have updated answer with sample code, just for your knowledge
try..
C#
if (!String.IsNullorEmpty(Convert.ToString((dataRow["HwareCost"])))
{
 itAreaList.HwareCost = Convert.ToDecimal((dataRow["HwareCost"]);
}
else
{
itAreaList.HwareCost=null;
}
 
Share this answer
 

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