c# - Deleting column causes column does not exist in table error -
i'm populating dataset sql query. fetch columns in query need them further queries. delete columns after im' done them, column "columnname" doesn't exist in "dataset" error when try bind dataset gridview.
any ideas cause?
edit: here's code actual error message: "column 'bid' not belong table results."
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 , 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 '%"+day+"%' , 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 > 0 ) { for(int = 0; < 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 , rs.bid = @bid , b.bid = rs.bid "+ "group 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 seats booked, remove row 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 results gridview gridview1.datasource = _results; gridview1.databind(); // dispaly results panel results.visible = true; } else { message.text = "no bus found"; message.visible = true; }
}
gridview1 markup:
<asp:gridview id="gridview1" runat="server" onrowcreated="gridview1_rowcreated" enableviewstate="false" gridlines="horizontal"> <columns> <asp:templatefield> <itemtemplate> <asp:literal id="radiobuttonmarkup" runat="server"></asp:literal> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
database columns:
route: rid, from_location, to_location
bus: rid, bid, name, ac, volvo, sleeper, fare, max_seats, runs_on, departure
reservations: tid, pid, bid, seats_booked, date_booked, date_journey
edit: noticed, if remove column 'max', works, see gridview without column, removing 'bid' somehow gives me error.
edit: solved! trying access bid column in rowcreated method, caused this. helped.
i'd suggest adding alias "bid" b.bid in first sql statement. maybe that's it's looking for.
Comments
Post a Comment