How does the Add Method from the NameObjectCollectionBase collection behave? - collections

Why does the Add method on the NameObjectCollectionBase insert at index position 0?
Is there a way to insert the new item at the end of the collection?
I'm facing this problem in .Net 2.0
I'm not allowed to change the collection.

NameObjectCollectionBase is based on a Hashtable/Dictionary, hence it is 'unsorted' by nature.
I think you are looking for a SortedList.
Update: If you cannot change the class, you are out of luck. Just remember after each add/insert the 'positions' may change.

Related

Use custom value on symfony2 choice form type using choice_list

I'm trying to use custom values in a choice form type which gets its data from a database query that needs post-processing. For this reason I opted to use the choice_list option and extending Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList\ChoiceList. The problem is that I need custom index/value for the resulting dropdown instead of the default 0-indexed style. 0-index doesn't work form me as I will access the values using Javascript and need the data I retrieved from the database.
I already tried replacing the createIndex() method in the ChoiceList class but to no avail :-(
Any tips?
I can't believe it...I have tried the whole day and couldn't find the answer. 5 Minutes after having published the question, I solved it.
For future research:
You need to overwrite the createValue() method in the Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList class.

EPiServer: Can I set a dynamic property from code-behind?

I tried to set it as a normal page-property, but no luck.
Guess I could use the DynamicProperty class but I really want to avoid this because of the no-cache issue.
Suggestions anyone?
AFAIK the only way to do this is with the DynamicProperty class. If you look at the documentation on the indexer property on the PageData object it says:
Note! Using this indexer will use the Pre and Post handlers for property lookup. I e return values are not guaranteed to belong to the page, but may be dynamic properties, "fetch-data-from"-data etc. To get data guaranteed to belong to this page, use the GetValueand SetValue methods.
Also note that setting values with this indexer will only set values that acually belong to the page, i e you may get a valid value by reading from the indexer, but trying to set a new value for the same index may yield an exception since the value does not exist in the page.
You will need to use the DynamicProperty class:
DynamicProperty myDynProp = DynamicProperty.Load(CurrentPage.PageLink, "PropertyName");
myDynProp.PropertyValue.Value = "new value";
myDynProp.Save();
Alternatively, you could circumvent the Dynamic Property using an idea Joel discusses here

Is it possible possible to add an object to the ObjectDataSource's InsertParameters collection? [as opposed to a string]

I'm invoking the ObjectDataSource InsertMethod programmatically. I've tried so save a couple of text boxes' value to the DB using the InsertParameters.Add() method, and it worked perfectly.
SqlDataSource1.InsertParameters.Add("CoName", CompanyNameBox.Text);
SqlDataSource1.InsertParameters.Add("Phone", PhoneBox.Text);
SqlDataSource1.Insert();
Now, given that I have an important number of variables, I've regrouped those variables in one object.
Now, it looks like there's no overload method that allow me to add a parameter accepting an object to the collection.
What the best way to do that? Any other alternative?
Thanks for helping.
All your paramaters will have to be provided one by one in your code, that is to say one call to add a parameter per property of your custom object.

How to persist a change in a DBML

I have a table of users called Users
And a view called UsersActive that filters out deactivated users.
When I create my DBML, I drag the Users table in, then I change the property on the table to point to UsersActive.
This works well, until the DBML gets re-created.
Does anyone know how to fix this?
I've tried overriding the
[Table(Name="dbo.Users")]
attribute in a partial class but get the error:
Duplicate 'Table' attribute
Does anyone know how to go about this?
Thanks in advance!
-Ev
You should just be able to add the View to the DBML, just like a table...yes?
Update: No, it will probably not maintain the relationships -- views don't have relationships.
It sounds like your goal is to query active users in a simple way, without having to specify the criterion in each query?
What you might do then is to have a repository class with a method of GetUsers(). That method does the Linq query and ensures that the active criterion is always there.
Perhaps the method would have a signature of Respository.GetUsers(bool includeDeativated = false). Calling GetUsers() without arguments will not return deactivated, but you can override it if desired.

ASP.Net: Is it possible to skip databinding of an element if an error occurs?

I use a lot of repeaters for different elements of our sites, and I've always wondered if there was a way to have the repeater skip an element if an exception occurs instead of having the whole page crash?
In particular, I've inherited a system from another developer that using a similar design, however he didn't include any kind of validation for his business objects, and it a single property is missing, the whole thing goes up in smoke.
Any suggestions?
Thanks in advance!
The simplest suggestion I can offer is the check the validity of the data before it's passed to the repeater. I don't believe there's any way to get the stock repeater to skip a data element on error.
The other approach is to build your own repeater, inheriting from the base Repeater, to add that functionality but I've no sample code to offer. Perhaps someone else may be able to help there.
The way I see it, you have at least three options.
You could create a custom repeater control that inherits System.Web.UI.WebControls.Repeater and override the databinding behaviour to be more try-catchy (probably fail silently on databinding errors). You couldd then easily replace all instances of the standard Repeater with this new one.
You could filter your datasources before databinding to remove items you know are going to cause problems beforehand. This option may be quite laborious and something of an iterative process.
You could try adding default values to the business objects, so that the properties you're binding to return a default instance rather than null (not nice either).
That's my thoughts anyway.
One question - you say "when a property is missing". Do you mean he's using a style of databinding syntax that offers no compile-time checking and is referencing properties that don't exist, or is referecing properties that are null?
Edit
OK, so you're referencing properties that are null. If you have access to the code for the business objects you could modify them so they return a new, non-null instance (this is the third option I gave).
You don't say if you're using .net 3.5, but I'll assume you are. You could add a new property "IsValidForDataBinding" on to each of your business objects. In the getter logic you could check each of the necessary properties and sub-objects to check for validity, non-nullness etc and return a bool. When you come to bind your repeater, write a simple linq statement that filters-out the invalid items (i.e. where IsValidForDataBinding = false). Having said that, I still think that writing a derived repeater control could be your easiest option.
Have you tried using string.isnullorempty("the string") to check for a value before referencing the property?
Here's a reference: MSDN

Resources