views:

139

answers:

2

We have a Sharepoint site which uses search.

We get the following error:

Unable to validate data.   at 
System.Web.Configuration.MachineKeySection.EncryptOrDecryptData
(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, 
IVType ivType, Boolean useValidationSymAlgo) 
   at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)

After a bit of testing we have found that the error occurs when we use DateKeyNames on a GridView control.

Not sure why there should be any combination between Search, this error and DataKeyNames.

Any ideas?

Update: Here is some code

[Guid("8891224e-e830-4ffa-afbd-f789583d8d14")]
    public class TestErrorGridView : System.Web.UI.WebControls.WebParts.WebPart
    {
        Control ascxToAdd;
        public TestErrorGridView()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

        }

        protected override void CreateChildControls()
        {


            base.CreateChildControls();
            Table test = new Table();
            TestBindObject t1 = new TestBindObject() { ID = 1, Name = "Test" };
            List<TestBindObject> l1 = new List<TestBindObject>();
            l1.Add(t1);
            SPGridView testGrid = new SPGridView() { AutoGenerateColumns = false};
            BoundField header = new BoundField();
            header.DataField = "ID";
            BoundField name = new BoundField();
            name.DataField = "Name";
            testGrid.Columns.Add(header);
            testGrid.Columns.Add(name);
            testGrid.DataSource = l1;
            testGrid.DataBind();
            // If you comment out this line search works
            testGrid.DataKeyNames = new string[] { "ID" };
            this.Controls.Add(testGrid);

            base.CreateChildControls();
            SPGridView testGrid = new SPGridView() { AutoGenerateColumns = false, EnableViewState=false };

            testGrid.DataKeyNames = new string[] { "testid" };  

            this.Controls.Add(testGrid);

        }
    }

public class TestBindObject
{
   public int ID { get; set; }
   public string Name { get; set; }
}

UPDATE

This error occurrs on the developer machines, so it is not realated to machine keys being different on different machines in a cluster.

One of the developers has MOSS installed, he does not get the error. The developers who have just WSS installed get the error.

UPDATE 2

Have added datasource to code

+1  A: 

I'm going to throw out a guess here since the code where you set the GridView's datasource is missing as well, but here goes...

It probably has something to do with the order in which you are setting the GridView's properties. My guess is that you need to set it in this order:

  1. Create your GridView
  2. Set the GridView's datasource (so that it has data)
  3. Set DataKeyNames
  4. Call GridView.DataBind()

Step 3 and 4 are the steps I am not sure about. You may have to DataBind and then set DataKeyNames.

Kit Menke
Thanks for the reply, this error happens without us adding the data source
Shiraz Bhaiji
Well if you don't have a datasource, how can you expect to use the DataKeyNames property?
Kit Menke
@Kit, we were trying to strip away everything that could cause the problem. I have updated the code with databinding / data source. We get the same error.
Shiraz Bhaiji
+1 Thanks for the help, we ended up changing to use DataTables
Shiraz Bhaiji
A: 

We eventually solved this by following the example in this link:

http://msdn.microsoft.com/en-us/library/bb466219.aspx

I think that is was binding to a data table rather than a list that made the difference.

The fact that Sharepoint grids do not allow autogenerated columns appears to be one of the factors leading to this problem.

Shiraz Bhaiji