Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to save List of object in to session variable?

here getting the error
Unable to cast object of type EMONBL.PersonList to type System.Data.DataSet'.

C#
using (PersonList pList = new PersonList())
            {
                Session["name"] = (PersonList)pList.GetRoleTypePersonDetails();

                DataTable dt = new DataTable();
                DataSet ds=new DataSet();
                try
                {
                    if (Session["name"] != null)
                    {
                        ds = (DataSet)Session["name"];
                        DataView dv = new DataView(ds.Tables[0]);
                        string s = "User";
                        dv.RowFilter = "name=" + s + "";
                        DataTable dt1 = new DataTable();
                        dt1 = dv.ToTable();
                        DropDownList1.DataSource = dt1;
                        DropDownList1.DataTextField = "lastName";
                        DropDownList1.DataValueField = "ID";
                        DropDownList1.DataBind();
                    }
Posted
Updated 5-Jun-12 3:36am
v2

1 solution

Unable to cast object of type EMONBL.PersonList to type System.Data.DataSet'.
C#
//...
Session["name"] = (PersonList)pList.GetRoleTypePersonDetails();
//...
ds = (DataSet)Session["name"];

You get this error as there is a datatype mismatch. Earlier, you stored an object of type 'PersonList' in Session. But, later, you try to use the same session and cast it into a DataSet.
DataSet class is different from PersonList object and hence compiler fails to convert/cast it as asked and throws an error.


It looks like you want to have a datasource for your dropdown. Convert or have the PersonList of datatype that can be a datasource directly or else, convert the data into a format that can be used as a datasource.
 
Share this answer
 
Comments
kamal-kondi 6-Jun-12 1:58am    
thankss !!!!1
kamal-kondi 6-Jun-12 2:01am    
but here i want convert session object into dataset or datatable but it was not converted.i getting the session value from person list class ,plz help me how to convert session var into dataset or datatabel
Sandeep Mewara 6-Jun-12 2:03am    
Thats what I said, you cannot convert any object into a dataset/datatable. You can cast back into the same object type which is stored in session.

Check your PersonList structure, use the values to construct a new dataset that can be used. You know your PersonList details, so you can do it, others cannot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900