R – Sharepoint search fails when using DataKeynames

gridviewmosssearchsharepointwss

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

Best Answer

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.