Let’s say you’re getting data from a database, and some of the data is null (maybe it’s an integer). A very common approach is to iterate through a table:
dtEmployees = emp.Get(sqlGetEmployees);
foreach (DataRow drEmp in dtEmployees.Rows)
...
ptt.Fieldname = drEmp["Fieldname"] != DBNull.Value ?
int.Parse(drEmp["Fieldname"].ToString()) :0;
That will populate the page with a ‘0’ for that item. And, if that’s a decimal:
ptt.Fieldname = drEmp["Fieldname"] != DBNull.Value ?
decimal.Parse(drEmp["Fieldname"].ToString()) :0;
However, did you know that, with a decimal, you can can also format it like ‘0.00’? All you need to do is this:
ptt.Fieldname = drEmp["Fieldname"] != DBNull.Value ?
decimal.Parse(drEmp["Fieldname"].ToString()) :0.00m;
That’s all there is to it.
All Things DotNet Discussed – Winforms/ASP.Net/SharePoint/WPF
Leave a reply
You must be logged in to post a comment.