[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error: "Specified cast not valid" on ItemDataBound method

Posted on 2006-05-24
5
Medium Priority
?
299 Views
Last Modified: 2007-12-19
Hi,
I'm getting a "Specified cast not valid" error on running the following code; I've noted where the error pops up.  Any idea why it's happening?

Public Sub rptQuestions_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptQuestions.ItemDataBound
        Dim lt As ListItemType = e.Item.ItemType
        If lt = ListItemType.Item OrElse lt = ListItemType.AlternatingItem Then
            Dim dv As DataRowView = CType(e.Item.DataItem, DataRowView)  <--- ERROR OCCURS HERE
            If Not dv Is Nothing Then
                Dim nestedRepeater As Repeater = CType(e.Item.FindControl("rptAnswers"), Repeater)
                If Not nestedRepeater Is Nothing Then
                    nestedRepeater.DataSource = dv.CreateChildView("Answers")
                    nestedRepeater.DataBind()
                End If
            End If
        End If
    End Sub

Thanks,
0
Comment
Question by:tmccrank
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Sam_Jayander
ID: 16757518
Hi,

Instead of casting the e.Item.DataItem to DataRowView, cast it to DataRow.

on ItemDataBound, e.Item.DataItem contains each row which is bound.

HTH,
--Sam.
0
 

Author Comment

by:tmccrank
ID: 16761865
Hi Sam,

Thanks, but I'm getting the same error casting it as a DataRow.
Any other ideas?

Jens
0
 

Author Comment

by:tmccrank
ID: 16762520
I should note that the code I posted above is based on an article (http://idunno.org/displayBlog.aspx/2004103001) where records were extracted from a DataSet.  I'm getting my info from two ArrayLists, not a DataSet (one for the outer repeater and the other for the inner one).  This may be where the difficulty in casting the DataItem is popping up.  Maybe not.

Please note I've increased the point value for this question, thanks for any help in advance.

J
0
 
LVL 5

Accepted Solution

by:
Sam_Jayander earned 2000 total points
ID: 16763296
Hi Jens,

As you see in the example at the refered url, when the item is bound to dataset, the itemdata in itemdataBound event is cast to DataRowView.

In our case, you are binding an ArrayList.

If you have added "string" values to ArrayList  as like,
   ArrayListName.Add("test1");
   ArrayListName.Add("test2");
then, cast it to String.

OR, If you have added, objects as like,
   ArrayListName.Add(obj1);
   ArrayListName.Add(obj2);
then, cast it to the type of object.  

i.e, if you have added a simple class as like

public class Person
{
      public string name;
      public string initial;
      public int age;
}

and if you have added a object as like
   Person per = new Person();
   per.name="test";
   per.initial = "T";
   per.age = 24;
   ArrayListName.Add(per);
then, you should cast it to the type "Person". (The base object type.)

HTH,
Sam.
0
 

Author Comment

by:tmccrank
ID: 16764321
Sam,

Thanks, that did it.  I was getting so caught up in the code for how to bind the repeater to a DataSet and trying to adapt it to an ArrayList that I couldn't see the forest for the string trees.  Here's how my code ended up:

    Public Sub rptQuestions_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptQuestions.ItemDataBound
        Dim lt As ListItemType = e.Item.ItemType
        If lt = ListItemType.Item OrElse lt = ListItemType.AlternatingItem Then
            Dim strAnswers As String = CType(e.Item.DataItem, String)
            If Not strAnswers Is Nothing Then
                Dim nestedRepeater As Repeater = CType(e.Item.FindControl("rptAnswers"), Repeater)
                If Not nestedRepeater Is Nothing Then
                    Dim alAnswers As ArrayList
                    alAnswers = DA.PopulateAnswers(CInt(Request.QueryString(0)))
                    nestedRepeater.DataSource = strAnswers
                    nestedRepeater.DataBind()
                End If
            End If
        End If
    End Sub

I've got a couple of other small issues to iron out, but this one is done!

Jens
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question