Iterate through the NewValues collection and HTML encode all
<p>I have a Gridview and I try to Iterate through the NewValues collection and HTML encode all.</p>

<p>I am following <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.aspx" rel="nofollow">MSDN CODE</a>.... using their code (posted here) I receive an error:</p>

<pre><code>Collection was modified; enumeration operation may not execute.
</code></pre>

<p>I would like ask you a Full simple example how to implement it, so beginners like me can start use this function.
PS: I posted a similar questions <a href="http://stackoverflow.com/questions/4591414/how-to-html-encode-values-in-a-gridview-when-updatating-error-collection-was-m">here</a> and people replied but I still do not understand it and i Would need a simple example.</p>

<p>I really appreciate for your help!</p>

<pre><code>    &lt;%@ Page language="C#" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;script runat="server"&gt;

  void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {

    // Iterate through the NewValues collection and HTML encode all 
    // user-provided values before updating the data source.
    foreach (DictionaryEntry entry in e.NewValues)
    {

      e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());

    }

  }

&lt;/script&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  &lt;head runat="server"&gt;
    &lt;title&gt;GridView RowUpdating Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;

      &lt;h3&gt;GridView RowUpdating Example&lt;/h3&gt;

      &lt;!-- The GridView control automatically sets the columns     --&gt;
      &lt;!-- specified in the datakeynames property as read-only.    --&gt;
      &lt;!-- No input controls are rendered for these columns in     --&gt;
      &lt;!-- edit mode.                                              --&gt;
      &lt;asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdating="CustomersGridView_RowUpdating"  
        runat="server"&gt;
      &lt;/asp:gridview&gt;

      &lt;!-- This example uses Microsoft SQL Server and connects  --&gt;
      &lt;!-- to the Northwind sample database. Use an ASP.NET     --&gt;
      &lt;!-- expression to retrieve the connection string value   --&gt;
      &lt;!-- from the Web.config file.                            --&gt;
      &lt;asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        connectionstring="&lt;%$ ConnectionStrings:NorthWindConnectionString%&gt;"
        runat="server"&gt;
      &lt;/asp:sqldatasource&gt;

    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
