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!

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

How to add dataset values instead of this arraylist in the below line arr.Add(chkCol0.Checked);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.