Microsoft KB Archive/320708

= PRB: Cannot edit data in grid from Windows CE-based device =

Article ID: 320708

Article Last Modified on 6/25/2004

-

APPLIES TO


 * Microsoft Pocket PC 2002 Software Standard Edition, when used with:
 * Microsoft Pocket Internet Explorer 2.0
 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q320708



SYMPTOMS
If the following conditions are true
 * You click the update link to modify data in an ASP.NET DataGrid control.

-and-
 * You view the grid on a device that is running Pocket PC 2002 in Pocket Internet Explorer.

you receive the following error message:

Server Error in '/ ' Application.

The View State is invalid for this page and might be corrupted.

Description: An unhandled exception occurred during the execution of the current web request.

Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The View State is invalid for this page and might be corrupted.

Source Error:

An unhandled exception was generated during the execution of the current web request.

Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): The View State is invalid for this page and might be corrupted.]

System.Web.UI.Page.LoadPageStateFromPersistenceMedium

System.Web.UI.Page.LoadPageViewState

System.Web.UI.Page.ProcessRequestMain

Version Information: Microsoft .NET Framework Version:1.0.3705.209; ASP.NET Version:1.0.3705.0



CAUSE
The view state that is returned from Pocket Internet Explorer is truncated.



WORKAROUND
Use the Microsoft Mobile Internet Toolkit in the Microsoft .NET Framework version 1.0 or the ASP.NET Mobile Controls in the .NET Framework version 1.1.

To download MMIT, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?FamilyID=ae597f21-b8e4-416e-a28f-b124f41f9768

The ASP.NET Mobile Controls are built into the Microsoft .NET Framework 1.1. To download the .NET Framework 1.1, visit the following Microsoft Web site:

http://msdn.microsoft.com/netframework/downloads/redist.aspx

You can also override the default view state handling. To do this, override the Page.SavePageStateToPersistenceMedium and the Page.LoadPageStateFromPersistenceMedium methods, and then save the view state on the server. For more information about Page.SavePageStateToPersistenceMedium, visit the following Microsoft Developer Network Web site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuipageclasssavepagestatetopersistencemediumtopic.asp



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Create a new ASP.NET Web application in Microsoft Visual Basic .NET. Add a Web form to your project, and then switch to HTML view.  Delete any existing code in the .aspx page, and then add the following code to the Web form: <%@ Import Namespace=&quot;System.Data&quot; %> <%@ Import Namespace=&quot;System.Data.SqlClient&quot; %> 

SqlConnection myConnection;

protected void Page_Load(Object Src, EventArgs E)   { myConnection = new SqlConnection(&quot;server=(local);database=pubs;UID=USER;PWD=PASSWORD&quot;);

if (!IsPostBack) BindGrid; }

public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)   { MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;

BindGrid; for (int i = 0; i < e.Item.Cells.Count; i++) {           e.Item.Cells[i].Text = Server.HtmlDecode(e.Item.Cells[i].Text); Trace.Write(e.Item.Cells[i].Text); }   }

public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)   { MyDataGrid.EditItemIndex = -1; BindGrid; }

public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)   { String updateCmd = &quot;UPDATE Authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, &quot; + &quot;address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id&quot;;

SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);

myCommand.Parameters.Add(new SqlParameter(&quot;@Id&quot;, SqlDbType.NVarChar, 11)); myCommand.Parameters.Add(new SqlParameter(&quot;@LName&quot;, SqlDbType.NVarChar, 40)); myCommand.Parameters.Add(new SqlParameter(&quot;@FName&quot;, SqlDbType.NVarChar, 20)); myCommand.Parameters.Add(new SqlParameter(&quot;@Phone&quot;, SqlDbType.NChar, 12)); myCommand.Parameters.Add(new SqlParameter(&quot;@Address&quot;, SqlDbType.NVarChar, 40)); myCommand.Parameters.Add(new SqlParameter(&quot;@City&quot;, SqlDbType.NVarChar, 20)); myCommand.Parameters.Add(new SqlParameter(&quot;@State&quot;, SqlDbType.NChar, 2)); myCommand.Parameters.Add(new SqlParameter(&quot;@Zip&quot;, SqlDbType.NChar, 5)); myCommand.Parameters.Add(new SqlParameter(&quot;@Contract&quot;, SqlDbType.NVarChar,1));

myCommand.Parameters[&quot;@Id&quot;].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];

String[] cols = {&quot;@Id&quot;,&quot;@LName&quot;,&quot;@FName&quot;,&quot;@Phone&quot;,&quot;@Address&quot;,&quot;@City&quot;,&quot;@State&quot;,&quot;@Zip&quot;,&quot;@Contract&quot;};

int numCols = e.Item.Cells.Count; for (int i=2; i<numCols-1; i++) //skip first, second and last column {           String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text;

// Look for null values in required fields. if (i<6 && colvalue == &quot;&quot;) {               Message.InnerHtml = &quot;ERROR: Null values not allowed for Author ID, Name or Phone&quot;; Message.Style[&quot;color&quot;] = &quot;red&quot;; return; }

myCommand.Parameters[cols[i-1]].Value = Server.HtmlEncode(colvalue); }

// Append last row, converting true/false values to 0/1 if (String.Compare(((TextBox)e.Item.Cells[numCols-1].Controls[0]).Text, &quot;true&quot;, true)==0) myCommand.Parameters[&quot;@Contract&quot;].Value = &quot;1&quot;; else myCommand.Parameters[&quot;@Contract&quot;].Value = &quot;0&quot;;

myCommand.Connection.Open;

try {           myCommand.ExecuteNonQuery; MyDataGrid.EditItemIndex = -1; }       catch (SqlException exc) {           if (exc.Number == 2627) Message.InnerHtml = &quot;ERROR: A record already exists with the same primary key&quot;; else Message.InnerHtml = &quot;ERROR: Could not update record, please ensure the fields are correctly filled out&quot;; Message.Style[&quot;color&quot;] = &quot;red&quot;; }

myCommand.Connection.Close;

BindGrid; }

private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)   { if (e.Item.ItemType == ListItemType.EditItem) {           for (int i = 0; i < e.Item.Controls.Count; i++) {               if (e.Item.Controls[i].Controls[0].GetType.ToString == &quot;System.Web.UI.WebControls.TextBox&quot;) {                   TextBox tb = (TextBox)e.Item.Controls[i].Controls[0]; tb.Text = Server.HtmlDecode(tb.Text); }           }        }    }    public void BindGrid {       SqlDataAdapter myCommand = new SqlDataAdapter(&quot;select * from Authors&quot;, myConnection);

DataSet ds = new DataSet; myCommand.Fill(ds, &quot;Authors&quot;);

MyDataGrid.DataSource=ds.Tables[&quot;Authors&quot;].DefaultView; MyDataGrid.DataBind; }

   <ASP:DataGrid id=&quot;MyDataGrid&quot; runat=&quot;server&quot; Width=&quot;800&quot; BackColor=&quot;#ccccff&quot; BorderColor=&quot;black&quot; ShowFooter=&quot;false&quot; CellPadding=&quot;3&quot; CellSpacing=&quot;0&quot; Font-Name=&quot;Verdana&quot; Font-Size=&quot;8pt&quot; HeaderStyle-BackColor=&quot;#aaaadd&quot; OnEditCommand=&quot;MyDataGrid_Edit&quot; OnCancelCommand=&quot;MyDataGrid_Cancel&quot; OnUpdateCommand=&quot;MyDataGrid_Update&quot; DataKeyField=&quot;au_id&quot; OnItemDataBound=&quot;MyDataGrid_ItemDataBound&quot;> <Columns> <asp:EditCommandColumn EditText=&quot;Edit&quot; CancelText=&quot;Cancel&quot; UpdateText=&quot;Update&quot; ItemStyle-Wrap=&quot;false&quot; /> </Columns> </ASP:DataGrid> </li> Modify the SqlConnection string in the code as necessary for your environment.</li> Save the Web form.</li> View the page in the browser on a device that is running Pocket Internet Explorer on the Pocket PC 2002 operating system.</li> Click Edit in the first row of the data grid. You receive the error message that is listed in the &quot;Symptoms&quot; section of this article.</li></ol>

Keywords: kbstate kbprb KB320708

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.