我在a页面建立了一个DataTable,然后用Session保存了他。
在b页面我用DataTable myDT=new DataTable;
           myDT=(DataTable)Session["myCartTable"];
然后我想将这个myDT中的数据,放入新的表Order
然后我使用了如下代码:
SqlConnection myConnection=new SqlConnection("server=Localhost;uid=sa;pwd=;database=mlshop");
DataSet ds=new DataSet();
ds.Tables.Add(myDT);
string SqlText="";
for(int i=0;i<ds.Tables[0].DefaultView.Count;i++)
{
SqlText="insert into Order(PdtId,PdtName,Price,Quantity,total) values('"+ds.Tables[0].DefaultView[i]["ProdID"].ToString()+"','"+ds.Tables[0].DefaultView[i]["ProName"].ToString()+"','"+ds.Tables[0].DefaultView[i]["UnitPrice"].ToString()+"','"+ds.Tables[0].DefaultView[i]["ProdCount"].ToString()+"','"+ds.Tables[0].DefaultView[i]["TotalPrice"].ToString()+"')";
SqlCommand cmd=new SqlCommand(SqlText,myConnection);
try
{
cmd.ExecuteNonQuery();
}
catch
{

}
finally
{
cmd.Dispose();
}
}
--------------------------------------------------
但是系统运行到ds.Tables.Add(myDT);时告诉我,DataTable已属于另一个数据集。
请问哪里出现了问题,我该如何修改??

解决方案 »

  1.   

    DataTable myDT=new DataTable;
    DataSet ds=new DataSet();
    ds.Tables.Add(myDT);
    myDT=(DataTable)Session["myCartTable"];
    换下顺序试一试
      

  2.   

    如果顺序换了,系统就告诉我,myDT没有赋值,还是无法同于编译
      

  3.   

    这是我在a页面建立的datatable:DataSet ds = new DataSet();
    DataTable newDT=new DataTable("CartTable");
    ds.Tables.Add(newDT);
    DataColumn newDC;
    newDC=new DataColumn("ProdID",System.Type.GetType("System.Int32"));           //ID  0
    ds.Tables["CartTable"].Columns.Add(newDC); newDC=new DataColumn("ProdCount",System.Type.GetType("System.Int32"));       //数量  1
    newDC.DefaultValue=1;
    ds.Tables["CartTable"].Columns.Add(newDC); newDC=new DataColumn("ProName",System.Type.GetType("System.String"));         //名称   2
    ds.Tables["CartTable"].Columns.Add(newDC); newDC=new DataColumn("UnitPrice",System.Type.GetType("System.Double"));       //单价  3
    ds.Tables["CartTable"].Columns.Add(newDC);
                  
    newDC=new DataColumn("TotalPrice",System.Type.GetType("System.Double"));     //小计  4
    ds.Tables["CartTable"].Columns.Add(newDC); newDC=new DataColumn("IsDeleted",System.Type.GetType("System.Int32"));       //删除标记  5
    newDC.DefaultValue=0;                                                    
    ds.Tables["CartTable"].Columns.Add(newDC);---------------------------------------
    然后在我一楼贴出的那个代码中,无法执行for语句,ds.Tables[0].DefaultView.Count的值为0,请问该如何修改??