I have built a dataset named Transaction_Time
.
I called it on Page_Load
Transaction_Time tranTme = new Transaction_Time();
put it in the session.
Session["Transaction"] = tranTme;
Then I call that session and cast to dataset.
DataSet dstTranTime = (DataSet)Session["Transaction"];
I got the following error.
Unable to cast object of type 'Transaction_Time' to type 'System.Data.DataSet'.
[InvalidCastException: Unable to cast object of type 'Transaction_Time' to type 'System.Data.DataSet'.]
Tra开发者_C百科nsaction_Time.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\William29_11_2010\Transaction_Time.aspx.cs:47
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
It is OK for some other pages. But for some page, it doesn't.
Is it something wrong that I do or.. ???
You put in typeof(Transaction_Time) and try to get out typeof(DataSet). this will fail until Transaction_Time is not derived from DataSet. Try to read Transaction_Time instead of DataSet.
Transaction_Time tranTme = new Transaction_Time();
....
Session["Transaction"] = tranTme;
....
Transaction_Time dstTranTime = Session["Transaction"] as Transaction_Time;
if (dstTranTime == null)
System.Dignostics.Trace.WriteLine("Ups! Expecting Transaction_Time, but got {0}", Session["Transaction"] );
It looks like you may have two classes called Transaction_Time. If you have created a class called Transaction_Time and derived it from DataSet AND you have a class called Transaction_Time that is derived from, say, Page. Then the compiler may be confused about which you mean and you may occasionally be storing the page Transaction_Time rather than the DataSet Transaction_Time.
Be explicit when creating the Transaction_Time class and use the fully qualified name OR rename one of your classes so that the name is not ambiguous. That should solve your problem (assuming my assumptions are correct).
精彩评论