Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:15:16+00:00 2026-05-28T18:15:16+00:00

Heres the overall code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI;

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:15:17+00:00Added an answer on May 28, 2026 at 6:15 pm

    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>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Heres the markup: <div id='0203' class='threadWrapper'> <div class='littleme'> I am trying to send the
I am using this code and it is giving exception NSMutableArray *streams = (NSMutableArray
I'm new to jquery and javascript, and to web site developing overall, and I'm
Here is the code: #include <iostream> #include <time.h> using namespace std; #define ARR_LENGTH 1000000
I modified the code published on smbrown.wordpress.com which can extract the top tracks using
Using the table code below I was asked to draw out a list sorted
I'm implementing a forum system called rBoard. Code is viewable at http://github.com/radar/rboard . I've
Here is my plan, and goals: Overall Goals: Security with a certain amount of
Heres a screenshot to make it clear. I'm trying to figure out a robust
Heres the link: DAMNIE6TOHELL As you can see if viewed in glorious 'IE6-o-color', the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.