如何将对象列表保存到会话变量中 [英] How to save List of object in to session variable
问题描述
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'.
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();
}
推荐答案
无法将EMONBL.PersonList类型的对象转换为System.Data.DataSet类型."
//...
Session["name"] = (PersonList)pList.GetRoleTypePersonDetails();
//...
ds = (DataSet)Session["name"];
由于数据类型不匹配,您会收到此错误.之前,您在Session中存储了"PersonList"类型的对象.但是,稍后,您尝试使用相同的会话并将其转换为数据集.
DataSet类不同于PersonList对象,因此编译器无法按要求转换/广播它并引发错误.
看起来您想为下拉菜单提供数据源.直接转换或具有可直接作为数据源的数据类型的PersonList,或者将数据转换为可用作数据源的格式.
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.
这篇关于如何将对象列表保存到会话变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!