locked
Unable to cast object of type 'System.String' to type 'Student'. RRS feed

  • Question

  • Hey 

    I am facing above error while executing following code. Please suggest what should I do?

    public class Student
    {
        int id;
            string name;
        public int ID
        {
            set { id = value; }
            get { return id; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
          
        }
        public Student(int id, string name)
        {
            this.id = id;
            this.name = name;
            //
            // TODO: Add constructor logic here
            //
        }
    }

    ---------------
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = Session["val"].ToString();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Student s = new Student(int.Parse(TextBox1.Text),(TextBox2.Text));
            Session["val"] = TextBox1.Text;
            Response.Redirect("UserForm.aspx");
        }

    Wednesday, May 16, 2018 6:30 PM

All replies

  • Show the lines that generate this error. Seems that the shown code does not contain such cast. You probably have a ‘Student s = (Student)some_variable’, which does not work.

    Thursday, May 17, 2018 5:35 AM