无法将类型“object”隐式转换为“System.Data.DataTable”。存在一个显式转换(是否缺少强制转换?)
Cart =Session[ "My_ShoppingCart "];//这一句为红色;用的是vs2005
.cs部份代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class cart : System.Web.UI.Page
{
DataTable Cart;
string connstr = ConfigurationManager.ConnectionStrings[ "MyStoreConnectionString "].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (Session[ "My_ShoppingCart "] ==null)
{
Cart = new DataTable();
Cart.Columns.Add(new DataColumn( "TotalId ",System.Type.GetType( "string ")));
Cart.Columns.Add(new DataColumn( "ProductName ", System.Type.GetType( "string ")));
Cart.Columns.Add(new DataColumn( "Author ", System.Type.GetType( "string ")));
Session[ "My_ShoppingCart "] =Cart;
}
else
{
Cart =Session[ "My_ShoppingCart "];//这一句为红色;为什么呢?
}
}
}
------解决方案--------------------------------------------------------
Cart =(DataTable)Session[ "My_ShoppingCart "];//