title: Error when paging in GridView
body: <p>I have a GridView with two columns. The first shows a date and the second, the year of the date in the first column. So far everything works well but when I change page, the web fails with the message: "Object reference not set to an instance of an object."</p>
  
  <p>This is my code:</p>
  
  <p>Test.aspx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="qq_site_Test" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;TEST&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" 
              AllowSorting="True" AutoGenerateColumns="False" 
              EnableModelValidation="True" onrowcreated="GridView1_RowCreated" 
              onpageindexchanged="GridView1_PageIndexChanged" 
              onpageindexchanging="GridView1_PageIndexChanging"&gt;
              &lt;Columns&gt;
                  &lt;asp:CommandField ShowSelectButton="True" /&gt;
                  &lt;asp:CommandField ShowDeleteButton="True" /&gt;
                  &lt;asp:BoundField DataField="creationDate" HeaderText="creationDate" SortExpression="creationDate" /&gt;
                  &lt;asp:TemplateField HeaderText="Year"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"&gt;&lt;/asp:TextBox&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;                
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Test.aspx.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.Odbc;
  using System.Data;
  
  public partial class qq_site_Test : System.Web.UI.Page
  {
      static DataSet ds;
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              String connectionString = "DSN=kitchenmaster.es.qq-site;";
              String sqlQuery = "SELECT * FROM tblSystems WHERE ID &lt; 100";
  
              ds = new DataSet();
  
              OdbcConnection connection = new OdbcConnection(connectionString);
              OdbcCommand command = new OdbcCommand(sqlQuery, connection);
              OdbcDataAdapter adapter = new OdbcDataAdapter(command);
  
              try
              {
                  adapter.Fill(ds);
                  GridView1.DataSource = ds;
                  GridView1.DataBind();
              }
              catch (System.Exception ex)
              {
                  Response.Write(ex.Message);
              }
              finally
              {
                  if (connection.State == ConnectionState.Open)
                      connection.Close();
              }
          }
      }
  
      protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              try
              {
                  DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "creationDate");
                  e.Row.Cells[GetColumnByID("Year")].Text = dt.Year.ToString();
              }
              catch (System.Exception ex)
              {
                  Response.Write(ex.Message);             
              }
          }
      }
  
      protected int GetColumnByID(String columnName)
      {
          foreach (DataControlField column in GridView1.Columns)
          {
              if (column.HeaderText == columnName)
                  return GridView1.Columns.IndexOf(column);
          }
  
          return -1;
      }
  
      protected void GridView1_PageIndexChanged(object sender, EventArgs e)
      {
          GridView1.SelectedIndex = -1;
      }
  
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          try
          {
              if (e.NewPageIndex != -1)
              {
                  GridView1.PageIndex = e.NewPageIndex;
                  GridView1.DataSource = ds;
                  GridView1.DataBind();
              }
          }
          catch (System.Exception ex)
          {
              Response.Write(ex.Message);
          }
      }
  }
  </code></pre>
  
  <p>Can you help me?</p>