在将对象序列化为流时,我遇到了以下错误。 [英] I am getting following error while serializing the object into steams.

查看:94
本文介绍了在将对象序列化为流时,我遇到了以下错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Type 'System.Data.DataRow' in Assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable










DataTable mytable = new DataTable("mytable");
        getMaxWorkID = Convert.ToInt32(objSQL.ExecuteSclar("select ISNULL(Max(workId),0) as workId from T_cuttingList"));
        con.Open();
        SqlCommand cmd = new SqlCommand("sp_cuttingMeasurement", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@mode", "selectHead");
        cmd.Parameters.AddWithValue("@workId", getMaxWorkID);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dtHead = new DataTable();
        da.Fill(dtHead);
        con.Close();
        int sno = 0;
        DataRow dr = null;
        if (dtHead.Rows.Count > 0)
        {
            
            foreach (DataRow rows in dtHead.Rows)
            {
                // Head to add mytable **** START ***
                if (ViewState["Row"] != null)
                {
                    mytable = (DataTable)ViewState["Row"];
                    if (mytable.Rows.Count > 0)
                    {
                        dr = mytable.NewRow();
                        sno = sno + 1;
                        dr["SNo"] = sno;
                        dr["Type"] = rows["Type"].ToString();
                        dr["Size"] = rows["Size"].ToString();
                        dr["Height"] = string.Empty;
                        dr["Nos"] = string.Empty;
                        dr["typeval"] = string.Empty;
                        dr["Sqft"] = string.Empty;
                        mytable.Rows.Add(dr);
                        ViewState["Row"] = mytable; 
                    }  
                }
                else
                {
                    mytable.Columns.AddRange(new DataColumn[7] { new DataColumn("SNo", typeof(string)),
                            new DataColumn("Type", typeof(string)),
                            new DataColumn("Size", typeof(string)),
                            new DataColumn("Height", typeof(string)),
                            new DataColumn("Nos", typeof(string)),
                            new DataColumn("typeval", typeof(string)),
                            new DataColumn("Sqft",typeof(string)) });

                    dr = mytable.NewRow();
                    sno = sno + 1;
                    dr["SNo"] = sno;
                    dr["Type"] = rows["Type"].ToString();
                    dr["Size"] = rows["Size"].ToString();
                    dr["Height"] = string.Empty;
                    dr["Nos"] = string.Empty;
                    dr["typeval"] = string.Empty;
                    dr["Sqft"] = string.Empty;
                    mytable.Rows.Add(dr);
                    ViewState["Row"] = mytable; 
                }
                // Head to add mytable **** END ***

                // Child Parent to add mytable **** START ***
                int headId = 0;
                headId = Convert.ToInt32(rows["Id"].ToString());
                ViewState["workId"] = rows["workId"].ToString();
                con.Open();
                SqlCommand cmd1 = new SqlCommand("sp_cuttingMeasurement", con);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@mode", "getparent");
                cmd1.Parameters.AddWithValue("@cId", headId);
                SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
                DataTable dtparent = new DataTable();
                da1.Fill(dtparent);
                con.Close();

                if (dtparent.Rows.Count > 0)
                {
                    foreach (DataRow drparent in dtparent.Rows)
                    {
                        if (ViewState["Row"] != null)
                        {
                            mytable = (DataTable)ViewState["Row"];
                            if (mytable.Rows.Count > 0)
                            {
                                dr = mytable.NewRow();
                                dr["SNo"] = string.Empty;
                                dr["Type"] = string.Empty;
                                dr["Size"] = drparent["width"].ToString();
                                dr["Height"] = drparent["height"].ToString();
                                dr["Nos"] = drparent["nos"].ToString();
                                dr["typeval"] = drparent["extra"].ToString();
                                dr["Sqft"] = drparent["sqft"].ToString();
                                mytable.Rows.Add(dr);
                                ViewState["Row"] = mytable;
                            }
                        }
                    }
                }
                // Child Parent to add mytable **** END ***


                // Draw / Shutter to add mytable **** START ***
                int typeId = 0; string AtMode = "";
                typeId = Convert.ToInt32(rows["typeId"].ToString());

                con2.Open();
                SqlCommand cmd8 = new SqlCommand("sp_cuttingMeasurement", con2);
                cmd8.CommandType = CommandType.StoredProcedure;
                cmd8.Parameters.AddWithValue("@mode", "selDrawShutterFMD3");
                cmd8.Parameters.AddWithValue("@typeId", typeId);
                cmd8.Parameters.AddWithValue("@Id", headId);
                SqlDataAdapter da8 = new SqlDataAdapter(cmd8);
                DataTable dtparent1 = new DataTable();
                da8.Fill(dtparent1);
                con2.Close();
                ViewState["parent1"] = null;
                if (dtparent1.Rows.Count > 0)
                {
                    foreach (DataRow drparent1 in dtparent1.Rows)
                    {
                        if (ViewState["Row"] != null)
                        {
                            mytable = (DataTable)ViewState["Row"];
                            if (mytable.Rows.Count > 0)
                            {
                                if (ViewState["parent1"] != null)
                                {
                                    dr = mytable.NewRow();
                                    dr["SNo"] = string.Empty;
                                    dr["Type"] = string.Empty;
                                    dr["Size"] = drparent1["width"].ToString();
                                    dr["Height"] = drparent1["height"].ToString();
                                    dr["Nos"] = drparent1["nos"].ToString();
                                    dr["typeval"] = drparent1["stype"].ToString();
                                    dr["Sqft"] = drparent1["sqft"].ToString();
                                    mytable.Rows.Add(dr);
                                    ViewState["Row"] = mytable;
                                    ViewState["parent1"] = drparent1;
                                }
                                else
                                {
                                    dr = mytable.NewRow();
                                    dr["SNo"] = string.Empty;
                                    dr["Type"] = "Draw/Shutter";
                                    dr["Size"] = rows["hwidth"].ToString();
                                    dr["Height"] = rows["hheight"].ToString();
                                    dr["Nos"] = string.Empty;
                                    dr["typeval"] = string.Empty;
                                    dr["Sqft"] = string.Empty;
                                    mytable.Rows.Add(dr);

                                    dr = mytable.NewRow();
                                    dr["SNo"] = string.Empty;
                                    dr["Type"] = string.Empty;
                                    dr["Size"] = drparent1["width"].ToString();
                                    dr["Height"] = drparent1["height"].ToString();
                                    dr["Nos"] = drparent1["nos"].ToString();
                                    dr["typeval"] = drparent1["stype"].ToString();
                                    dr["Sqft"] = drparent1["sqft"].ToString();
                                    mytable.Rows.Add(dr);
                                    ViewState["Row"] = mytable;
                                    ViewState["parent1"] = drparent1;
                                }
                            }
                        }
                    }
                }
                // Draw / Shutter to add mytable **** END ***


            }         
        }





我的尝试:





What I have tried:

By reading the exception, it looks like there is a dataRow in the object which is not serialized.

How can I identify the dataRow which is causing this problem in my code?

推荐答案

你是尝试将DataRow对象存储到ViewState中,并且ViewState中的任何内容都需要能够转换为文本,因为它最终存储在隐藏字段中。为此,您只能在ViewState中存储可以序列化的对象,并且无法序列化DataRow。你应该做的是创建自己的类,其中包含存储数据所需的属性(sno,类型,大小等),创建该类的实例,从数据行填充属性,然后将类存储在ViewState中。如果将[Serializable]属性添加到类定义中,那么它应该可以正常工作。
You're trying to store a DataRow object into the ViewState, and anything in the ViewState needs to be able to be converted to text as it is ultimately stored in a hidden field. To this end you can only store objects in the ViewState that can be serialised and DataRow can't be serialised. What you should do instead is create your own class with the properties you need to store the data (sno, type, size etc), create an instance of that class, populate the properties from the datarow, then store your class in the ViewState. If you add the [Serializable] attribute to your class definition then it should work ok.


这篇关于在将对象序列化为流时,我遇到了以下错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆