Deleting column causes column does not exist in table error
<p>I'm populating a DataSet from an Sql query. I fetch some extra columns in my query as i need them to for further queries. I delete those columns after im' done with them, but i get a <strong>Column "columnname" doesn't exist in "dataset"</strong> error when i try to bind the DataSet to a GridView.</p>

<p>Any ideas what could be the cause?</p>

<p>Edit: here's the code
The actual error message: "<strong>Column 'BID' does not belong to table results.</strong>"</p>

<pre><code>String command = "SELECT B.BID, B.NAME 'Name', B.FARE 'Fare', B.DEPARTURE 'Departure', B.MAX_SEATS 'MAX', NULL 'Seats Available' "+
                 "FROM ROUTE R, BUS B "+
                 "WHERE R.FROM_LOCATION = @from AND R.TO_LOCATION = @to ";
if(_ac) command += "AND B.AC = @ac ";
if(_volvo) command += "AND B.VOLVO = @volvo ";
if(_sleeper) command += "AND B.SLEEPER = @sleeper ";
command += "AND B.RUNS_ON LIKE '%"+day+"%' AND R.RID = B.RID";
SqlCommand cmd = new SqlCommand(command, con);

cmd.Parameters.AddWithValue("@from", fromValue);
cmd.Parameters.AddWithValue("@to", destValue);
if(_ac) cmd.Parameters.AddWithValue("@ac", _ac);
if(_volvo) cmd.Parameters.AddWithValue("@volvo", _volvo);
if(_sleeper) cmd.Parameters.AddWithValue("@sleeper", _sleeper);

SqlDataAdapter adapter = new SqlDataAdapter(cmd);
_results = new DataSet();

con.Open();
adapter.Fill(_results, "results");

DataTable dt = _results.Tables["results"];
if(dt.Rows.Count &gt; 0 ) {
    for(int i = 0; i &lt; dt.Rows.Count; i++) {
        DataRow row = dt.Rows[i];
        int max = Int32.Parse(row["MAX"].ToString());
        String bid = row["BID"].ToString();
        cmd.CommandText = "SELECT B.MAX_SEATS-SUM(RS.SEATS_BOOKED) 'REMAIN', RS.BID "+
                          "FROM RESERVATION RS, BUS B "+
                          "WHERE RS.DATE_JOURNEY = @date AND RS.BID = @bid AND B.BID = RS.BID "+
                          "GROUP BY B.MAX_SEATS, RS.BID";
        cmd.Parameters.AddWithValue("@date", dateValue);
        cmd.Parameters.AddWithValue("@bid", bid);

        SqlDataReader reader = cmd.ExecuteReader();
        if(reader.Read()) {
            max = Int32.Parse(reader["REMAIN"].ToString());
        }

        // If all seats are booked, remove the row from the table.
        if(max == 0) { dt.Rows.Remove(row); }
        else {
            row["Seats Available"] = max;
        }

        reader.Close();
        cmd.Parameters.Clear();
        dt.AcceptChanges();
    }

    if(dt.Columns.CanRemove(dt.Columns["BID"]))
        dt.Columns.Remove("BID");
    if(dt.Columns.CanRemove(dt.Columns["MAX"]))
        dt.Columns.Remove("MAX");

    dt.AcceptChanges();
    _results.AcceptChanges();

    // Bind the results to a GridView
    GridView1.DataSource = _results;
    GridView1.DataBind();

    // Dispaly Results Panel
    results.Visible = true;
}
else {
    message.Text = "No bus found";
    message.Visible = true;
}
</code></pre>

<p>}</p>

<p>GridView1 Markup:</p>

<pre><code>&lt;asp:GridView ID="GridView1" runat="server" onrowcreated="GridView1_RowCreated" EnableViewState="False" GridLines="Horizontal"&gt;
    &lt;Columns&gt;
    &lt;asp:TemplateField&gt;
        &lt;ItemTemplate&gt;
            &lt;asp:Literal ID="RadioButtonMarkup" runat="server"&gt;&lt;/asp:Literal&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;
&lt;/asp:GridView&gt;
</code></pre>

<p>Database columns:<br>
    <strong>ROUTE:</strong> RID, FROM_LOCATION, TO_LOCATION<br>
    <strong>BUS:</strong> RID, BID, NAME, AC, VOLVO, SLEEPER, FARE, MAX_SEATS, RUNS_ON, DEPARTURE<br>
    <strong>RESERVATIONS:</strong> TID, PID, BID, SEATS_BOOKED, DATE_BOOKED, DATE_JOURNEY</p>

<p>EDIT: Just noticed, if i only remove column 'MAX', it works, i see the GridView without that column, but removing 'BID' somehow gives me that error.</p>

<p><strong>EDIT:</strong> Solved! Was trying to access the BID column in the RowCreated method, which caused this. Thanks to all who helped.</p>
