user1062411
user1062411

Reputation: 65

Invalid Cast Exception ASP.NET

Heres the overall code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class partin : System.Web.UI.Page
{
private List<String> books = new List<String>();

void Page_PreRender()
{
    Item_Listbox.DataSource = books;
    Item_Listbox.DataBind();   
}

int SortASC(string x, string y)
{
    return String.Compare(x, y);
}

int SortDESC(string x, string y)
{
    return String.Compare(x, y) * -1;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        Header_Label.Text = "Welcome! Please select a book category.";
        Item_Listbox.DataSource = books;
        Item_Listbox.DataBind();

    }

}

protected void Fiction_Click(object sender, EventArgs e)
{

        Header_Label.Text = "Fiction Section";

        books.Add("Title: The Old Man and The Sea | Decription: An epic novel. | Price: 10 USD | Quantity: 3");
        books.Add("Title: A Game of Thrones | Decription: A tale of fire and ice. | Price: 15 USD | Quantity: 6");
        books.Add("Title: Dracula | Decription: A book about vampires. | Price: 5 USD | Quantity: 7");
        books.Add("Title: Twilight | Decription: An awful book. | Price: Free | Quantity: 1000");

        Item_Listbox.DataSource = books;
        Item_Listbox.DataBind();

        ViewState["books"] = books;

}


protected void Non_Fiction_Click(object sender, EventArgs e)
{
    Header_Label.Text = "Non-Fiction Section";



}
protected void Self_Help_Click(object sender, EventArgs e)
{
    Header_Label.Text = "Self Help Section";



}

protected void Sort_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "Sort")
    {
        switch (e.CommandArgument.ToString())
        {
            case "ASC":
                books.Sort(SortASC);
                break;
            case "DESC":
                books.Sort(SortDESC);
                break;
        }

    }
    if (ViewState["books"] == null)
        ViewState["books"] = new string[0];

    Item_Listbox.DataSource = new List<string>((string[])ViewState["books"]);
    Item_Listbox.DataBind();  
}



}

The Invalid Cast Exception being thrown here:

Item_Listbox.DataSource = new List<string>((string[])ViewState["books"]);

I'm pretty new to ASP.NET so I'm lost as to what may causing it, fixes welcome!

Upvotes: 1

Views: 1684

Answers (3)

Restuta
Restuta

Reputation: 5903

I guess it's because you are doing

private List<String> books = new List<String>();
//...
ViewState["books"] = books;

and then you are trying to cast List<string> to string[]

Item_Listbox.DataSource = new List<string>((string[])ViewState["books"]);

Rewriting last line in the following way should solve the issue:

Item_Listbox.DataSource = (List<string>)ViewState["books"];

Or even

Item_Listbox.DataSource = ViewState["books"];

And second place where you have following code:

if (ViewState["books"] == null)
        ViewState["books"] = new string[0];

If statement results to false, since ViewState was already set after button click, but in general I would suggest to be consistent with the data structures you're using and change this code to the following:

if (ViewState["books"] == null)
        ViewState["books"] = new List<string>();

Upvotes: 2

David Merriman
David Merriman

Reputation: 6086

ViewState["books"] is List<string>, not string[]. Remove the (string[]) and it should work. In fact, you can probably remove the new List( ), too.

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

You have 2 places where you set "books": ViewState["books"] = books; where books = new List<String>();.

ViewState["books"] = new string[0];

Upvotes: 0

Related Questions