Archive for January, 2012

Unable to cast object of type ObservableCollection

I break out some Silverlight coding from time to time, to keep the rust off. And most of the time it goes OK – you get the occasional error that come out of nowhere. Like this dreaded one:

Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection' exception

Where did that come from? I had bound a collection of MyGroup items to a ListBox. I later wanted to retrieve the items that were selected by using the SelectedItems collection:

ObservableCollection<MMLService.MyGroup> MyItem = 

When I tried to cast the ListBox.SelectedItem collections to the type of object I had bound to it, I received this error:

Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection`1[System.Object]' to type 

My attempts to cast the collection failed. But, I managed to skirt the error by using an Enumerator:

List<Int64> Groups = new List<Int64>();
IEnumerator<object> MyEnum = (IEnumerator<object>)SecurityGroupGroups.SelectedItems.GetEnumerator();
while (MyEnum.MoveNext())
    MMLService.MyGroup Temp = (MMLService.MyGroup)MyEnum.Current;

That can’t possibly be the best way to get around that exception. I know this with all my heart. That said, it worked, and I wanted to get around the issue and onto the rest of the code. And for the curious, the error occurred in Silverlight 4.

Leave a comment

Fixing “A potentially dangerous Request.Form value” exception

I was merrily converting a site from .NET 2.0 to .NET 4.0 when…

Yes, I did say I was upgrading from 2.0 to 4.0.

Anyways, when I did this and used a page that allowed HTML elements to be entered in a TextBox, I got the oh-so helpful error “A potentially dangerous Request.Form value was detected…”

No problem, I would just add the ‘ValidateRequest=”false” ‘ tag at the top of the page and get back to business. But business was not happening, because I added the tag, and nothing happened. Luckily, a little browsing found the solution, which was editing the web.config file and adding the requestValidationMode:

    <httpRuntime requestValidationMode="2.0"/>

That did the trick.

Leave a comment

Fixing ThreadAbortException when using Response.End

I was working with code that allowed the user to save an Excel file by clicking on a link on a web page. The user was then prompted to open, save, or cancel the file. This little code snippet accomplishes this, the MyStream object being a MemoryStream object:

Response.ContentType = "application/";
Response.AddHeader("Content-Disposition", "ScottsCoolFile.xls");
Response.BinaryWrite(MyStream ().GetBuffer());

The problem with this code is that is throws a ThreadAbortException on the call to Response.End(). I don’t know about you, but this is not what I want happening. Exceptions = bad.

A little research led me to the HttpApplication.CompleteRequest method. So I replaced the last line of the code above, and I then had:

Response.ContentType = "application/";
Response.AddHeader("Content-Disposition", "ScottsCoolFile.xls");

Voila, same functionality, but no exception.


Deleting records from a Telerik RadGrid not updating the pager

When I was deleting rows from the Telerik RadGrid, the page count used for paging on the bottom left wasn’t updating. So if I had 21 records, with a setting of 10 records per page, I would see 3 pages. If I deleted 10 records, I would expect that the pages listed would be reduced to 2, but it wasn’t – it was staying at three. As Mr. Smith from Lost in Space would say, “The pain, the pain”.

My solution was to create a global page variable to hold the latest number of rows:

int currentRows  = 0;

In the function that loads the grid (creatively called LoadGrid), I added code to update the currentRows  variable with the latest count of items from the database. So whenever the count was updated, my code would detect the latest count of items:

List<MyItem> Items = GetThoseObjectsDude();
 currentRows = Items.Count;

Finally, during the Page_PreRender event, I added a check to see if the count was updated. If so, then it updated the RadGrid’s VirtualItemCount property. When the RadGrid was rebound, the page count would be updated, too:

protected void Page_PreRender(object sender, EventArgs e)
 if (currentRows > 0)
 gridItems.VirtualItemCount = currentRows;
currentRows = 0;

1 Comment