title: Object reference not set to an instance of an object in gridview in asp.net
body: <p>I am getting this error on a row command in my gridview. Here is the code:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          SqlConnection con = new SqlConnection();
          con.ConnectionString = University.GetConnectionString();
          con.Open();
  
          string query = "select [CourseCode], [CourseNumber], [CourseName], [CRN], [Level], [Credit] from CourseTable where Term='" + MyGlobals.currentTerm + " " + MyGlobals.currentYear + "'";
  
          SqlDataAdapter adap = new SqlDataAdapter(query, con);
  
          DataTable tab = new DataTable();
  
          adap.Fill(tab);
  
          gCourses.DataSource = tab;
          gCourses.DataBind();
      }
  
  }
  
  
  
  protected void gCourses_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      // *** Retreive the DataGridRow
      int row = -1;
      int.TryParse(e.CommandArgument as string, out row);
  
      GridViewRow gdrow = gCourses.Rows[row];
      DataRow dr = ((DataTable)this.gCourses.DataSource).Rows[gdrow.DataItemIndex];
      string crn = dr["CRN"].ToString();
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>DataRow dr = ((DataTable)this.gCourses.DataSource).Rows[gdrow.DataItemIndex]; 
  </code></pre>
  
  <p>line throws the exception.</p>
  
  <p>What is wrong here? Thanks</p>