This question relates to ASP.Net 3.5 and C#
I'm building an RSS Feed from a database containing a lot of columns.
I need to format the data in a particular node and doing it inline is going to be very messy.
I'm aware I can pass all the parameters individually to a subroutine
<%# formatAddress(Container.DataItem["propertyName"],
Container.DataItem["propertyNumber"], ... ) %>
As there's going to be up to 20 of these columns, I'd rather pass the entire row.
<%# formatAddress(Container.DataItem) %>
This would be ideal then I can pick out the columns I want in code behind:
protected string FormattedAddress(object row)
{
DataRowView data = (DataRowView)row;
StringBuilder Address = new StringBuilder();
string Postcode = data["propertyPostcode"];
...
return Address.ToString();
}
I'm getting the error Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.DataRowView'.
Previously, I was using protected string FormattedAddress(DataRowView row) but that didn't work either.
Any clues?
Eventually found a couple of examples which led me to realise I should be casting to DbDataRecord.
I'm still passing <%# formattedAddress(Container.DataItem) %> but my function is now as follows:
protected string FormattedAddress(object dataItem)
{
DbDataRecord data = (DbDataRecord)dataItem;
string Postcode = data.GetValue(
data.GetOrdinal("propertyPostcode")).ToString();
...
return NicelyFormattedAddress;
}
Is this the best way to handle the data?