DataBinding: ‘System.Data.DataRowView’ does not contain a property with the name due to “.” / Period in ColumnName

Recently I met up with error “DataBinding: ‘System.Data.DataRowView’ does not contain a property with the name”,  on page with dynamically generated Gridview on it.Error was coming at the runtime at timing of binding  the data.

I first checked for the columns in the dataset before binding to Gridview , all the columns are properly generated. then I started looking for the code which is generating gridview on fly. I was using following event to bind the data dynamically

protected void ValueDataBinding(object sender, EventArgs e)
{
object bound_value_obj = null;
Control ctrl = (Control)sender;
IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;

bound_value_obj = DataBinder.Eval(data_item_container.DataItem, ColumnName); ( Exception was thown from above line while binding to column)

}

after much of debugging I found that ColumnName is  “Others…” and error was coming due to “.” / Period in ColumnName , the reason behind is  DataBinder.Eval Method Uses reflection to parse and evaluate a data-binding expression against an object at run time , so “.” can be treated with different , I havnt tried but I am sure it will throw the exception for other special chars also i.e. ;  , : ” ‘  etc.

This was solved by using

bound_value_obj =  DataBinder.GetPropertyValue(data_item_container.DataItem, ColumnName).ToString();


Advertisements

Setting css style programmatically in Asp.Net/C#

Sometimes we have requirements such a way that we need to change the elements and accordingly style dynamically under  div.

Suppose in one case  we need our div to be of width-size 800px and and can scroll if goes beyond.

for that on the front-end side (aspx) create <div id=”dvDStyle” runat=”server”>  </div>

then in code-behind  ( cs file) mention style using either of the way

1st one is mention it as Attribute for dvDStyle

2nd is mention style as separate property like Key,Value pair.

dvDStyle.Attributes.Add(“style”, “width:800px; overflow:scroll;”);

or

dvDStyle.Style[“width”] = “800px”;
dvDStyle.Style[“overflow”] =”scroll”;