This issue recently happen... and theres no fancy solution
Working in my developer job, i face a problem that was not difficult but it seems noone in internet have publish a solution, or maybe i dont know how to search. Anyway, there´s some control in ASP.NET named ObjectDataSource wich it´s used in tree layer applications, at the web layer, this control usually used in CRUD situtations making the connection between the web layer and the business layer calling the instances of business objects.
So problem is very specific... when ViewDetail is connected with the ObjectDataSource and there´s one field edited with a DropdownList, the binding event of this DropDown the framework makes two things: 1. Populate the items, 2. Assign the value the record already have (remember is an editing). The framework does it ok, but what if you allow records at database to be null at the same field this DropDown edits? Well it results in a disturbing exception, cause DropDown can not load items with a null value, and therefore its impossible to bind to null.
Ok, the solution is not ellegant but works,
1. Add a burned item to the DropDown wich has a -1 value
2. Implement this events at the DataSource, to change null by -1 when selecting, and reverse the operation when updating:
protected void DataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
DataTable result = e.ReturnValue as DataTable;
if (result.Rows.Count > 0 && result[0].IsNull("CompanyId")) result[0].CompanyId = -1;
}
protected void DataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
if ((int)e.InputParameters["CompanyId"] == -1) e.InputParameters["CompanyId"] = null;
}
No comments:
Post a Comment