0

I want to store ArrayList value in ViewState

But I am getting an error:

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

Code:

  private void bindGridView()
    {
        DbConnection.Open();
        OleDbCommand DbCommand = new OleDbCommand("select emp_id,emp_name,father_name,gender,designation,department,location from emp_master", DbConnection);
        OleDbDataAdapter Dbreader1 = new OleDbDataAdapter(DbCommand);
        DataSet dsemer = new DataSet();

        Dbreader1.Fill(dsemer);
        ArrayList arrList = new ArrayList();
        foreach (DataRow dtRow in dsemer.Tables[0].Rows)
        {
            arrList.Add(dtRow);
        }
        //Here getting an error
        ViewState["ds"] = arrList;
        EmpMasterGrid.DataSource = dsemer;
        EmpMasterGrid.DataBind();

        DbConnection.Close();
    }

Why I need to store ArrayList in ViewState?

By using that ViewState I will export selected gridview column to excel using the below code

 private void GetCheckBoxStates()
    {
        CheckBox chkCol0 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol0");
        CheckBox chkCol1 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol1");
        CheckBox chkCol2 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol2");
        CheckBox chkCol3 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol3");
        CheckBox chkCol4 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol4");
        CheckBox chkCol5 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol5");
        CheckBox chkCol6 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol6");
        ArrayList arr;

        if (ViewState["ds"] == null)
        {
            arr = new ArrayList();
        }
        else
        {
            arr = (ArrayList)ViewState["ds"];
        }
        arr.Add(chkCol0.Checked);
        arr.Add(chkCol1.Checked);
        arr.Add(chkCol2.Checked);
        arr.Add(chkCol3.Checked);
        arr.Add(chkCol4.Checked);
        arr.Add(chkCol5.Checked);
        arr.Add(chkCol6.Checked);
        ViewState["ds"] = arr;
    }

Any ideas? Thanks in advance. Edited!

Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63
user2500094
  • 1,033
  • 6
  • 23
  • 42

1 Answers1

0

As the error sais System.Data.DataRow is not serializable so you can not store it in ViewState.

I would suggest that you make a custom class that is marked as Serializable that contains all the values you need and save an ArrayList of those objects in the ViewState. You would populate these objects in your foreach loop with data from DataRow.

Example:

The data object

[Serializable]
public class Employee
{
    public int emp_id,
    public string emp_name,
    //other properties
}

In your bindGridView method

//other code
ArrayList arrList = new ArrayList();
foreach (DataRow dtRow in dsemer.Tables[0].Rows)
{
    arrList.Add(new Employee
    {
        emp_id = dtRow[0],
        emp_name = dtRow[1],
        //other properties
    };
}
ViewState["ds"] = arrList;
juhan_h
  • 3,965
  • 4
  • 29
  • 35