2011-02-07 14 views
0

私はデータベースに2つのテーブルを持ち、ビジュアルスタジオ(データセット)でデータモデルを使用し、2つのクラスを使用してこれらの2つのテーブルのメソッドとプロパティを格納します。テーブルアダプターはシリアル化可能とマークされていませんか?

私はこのエラーを取得するstateviewにリストを追加しようとすると、リストの中に何らかの理由でWebフォームから収集した情報を格納したい:

タイプ「『『TableAdapters 『』 TableAdapterのを』。』。 'Assembly' "、バージョン= 1.0.0.0、Culture =ニュートラル、PublicKeyToken = null 'はシリアル化可能とマークされていません。

私は既にクラスをシリアライズ可能としてマークしましたが、今はテーブルアダプターですか?ここに私のコードは次のとおりです。

[System.ComponentModel.DataObject] 
    [Serializable] 
    public class Example 
    { 
     int _example1 = new int(); 
     string _example2; 
     string _example3; 
     decimal _example4 = new decimal(); 

     public int example1 
     { 
      get { return _example1; } 
      set { _example1 = value; } 
     } 

     public string example2 
     { 
      get { return _example2; } 
      set { _example2 = value; } 
     } 

     public string example3 
     { 
      get { return _example3; } 
      set { _example3 = value; } 
     } 

     public decimal example4 
     { 
      get { return _example4; } 
      set { _example4 = value; } 
     } 

     private tblTestTableAdapter _testAdapter = null; 
     protected tblTestTableAdapter Adapter 
     { 
      get 
      { 
       if (_testAdapter == null) 
        _testAdapter = new tblTestTableAdapter(); 

       return _testAdapter; 
      } 
     } 

Webフォーム:

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
      } 
      else 
      { 
       example = (List<Example>)ViewState["Examples"]; 
      } 
     } 

     private List<Example> example; 
     public List<Example> GetExample() 
     { 
      return example; 
     } 

     protected void btnRow_Click(object sender, EventArgs e) 
     { 
      example = new List<Example>(); 
      Example e = new Example(); 
      e.example1 = Convert.ToInt32(txtE1.Text); 
      c.example2 = txtE2.Text; 
      c.example3 = txtE3.Text; 
      c.example4 = Convert.ToDecimal(txtE4.Text); 

      example.Add(e); 
      ViewState["Examples"] = example; 

      btnRow.Enabled = false; 

     } 

問題は何ですか?

答えて

0

クラスをSerializableとマークするときは、外部に公開されているすべてのクラスとそれに依存するオブジェクトクラスをすべてSerializableとマークする必要があります。これは、シリアライゼーションを実行しようとするどのプロセスでも、すべてのパブリック要素を適切にシリアライズしようとするからです。

FYIでは、テーブルアダプタはプロパティとフィールドではなく機能性を公開しているため、公開されることはありません。機能はシリアル接続を介して転送されません。あなたの例では、アダプターのパブリックな性質を削除することをお勧めします。

編集2:
あなたのコードを再読み込みし、シリアル化されたプロパティの保護レベルのドキュメントを探した後、私はここでthis link that describes the real problemに走りました。私は完全にこれを忘れてreadonlyプロパティをシリアル化することはできません、あなたのtableadapterプロパティは読み取り専用です。それにセットを提供すると、機能が開始されます。

編集:コードサンプル

[Serializable] 
public class MySerializableClass 
{ 
    public MySerializableClass() 
    { 

    } 

    // This string serializes ok 
    public string MyStringProperty { get; set; } 

    // Because this property is public in scope it must be serializable 
    // because it will be translated at a public scope. This will throw 
    // an exception 
    public myNonSerializableClass NotSerializableObject { get; set; } 

    // Because this property is private in scope, it will not be included 
    // in any serialization calls, so it will not throw an exception, but 
    // it will also not be available in whatever remote class calls it. 
    private myNonSerializableClass SerializableObject { get; set; } 

    // Because this property object is serializable in code it will be 
    // ok to make it public because it will natively serialize itself 
    public MyOtherSerializableClass OtherSerializableObject { get; set; } 

} 
+0

あなたはこの詳細を拡張することができますか? – user603605

+0

@ user603605 - ビットをデモするためのコメント付きのコードスニペットを追加しました –

+0

どのようにして「テーブルアダプターを公開するか」は、情報が収集されるテキストボックスを持っています。ユーザーがリストに追加を続行するか、続行するかを選択できます。私はテーブルアタッチメントが必要なので、最後に私のデータベースにリストを挿入することができます。 – user603605

関連する問題