Question

In my program I have a list view where I populate it with data from a database and by selecting one of the list view items I am redirected to a page where a second list view is to be created and populated according to the id of the item selected in the first list view. This id is obtained by means of a hyperlink from the first list view :

<%#Eval("CategoryName")%>

The code to populate the second list view is as follows:

 public partial class SubCategories : System.Web.UI.Page
{
    BASubCategory bs = new BASubCategory();
    void BindListView()
    {
        Guid cID = (Guid)ViewState["CatID"];
        List<SubCategory> subcatlist = (List<SubCategory>)bs.GetSubCategoryCategory(cID);
        SubCategoryListView.DataSource = subcatlist;
        SubCategoryListView.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ViewState["CatID"] = Request["id"];
        BindListView();
    }

}

When I try to run this I obtain an error at :

Guid cID = (Guid)ViewState["CatID"];

where I get the error "Specified cast is not valid"

Could anyone give some tips as to what I could do to solve this problem?

Was it helpful?

Solution

You could try:

Guid cID = new Guid(ViewState["CatID"].ToString());

ViewState appears to return an object which can't be cast to a Guid. If you try the above, you should be able to easily create the Guid from the string representation of "CatID" in the ViewState.

Also, in reply to your comment below, why not try this instead of the <asp:HyperLink>:

<a href='<%# ResolveClientUrl("~/SubCategories.aspx") %>?id=<%#Eval("CategoryId") %>'><%#Eval("CategoryName")%></a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top