I m populationing a DataSet from an Sql query. 我在问询中略加一栏,因为需要它们进一步查询。 我删除了这些栏目,但我只读到。 在试图将数据Set与GredView联系起来时,在“数据集”的错误中确实存在“栏目”。
任何可以产生的想法?
Edit: here s the code The actual error message: "Column BID does not belong to 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 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 > 0 ) {
for(int i = 0; i < 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());
iii
// If all seats are booked, remove the row from the table.
if(max == 0) { dt.Rows.Remove(row); iii
else {
row["Seats Available"] = max;
iii
reader.Close();
cmd.Parameters.Clear();
dt.AcceptChanges();
iii
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;
iii
else {
message.Text = "No bus found";
message.Visible = true;
iii
iii
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:如果只是删除第5栏,它就可操作,即见GredView,而没有该栏,但排除BID的一些how使我产生这一错误。
EDIT: Solved! Was trying to access the BID column in the RowCreated method, which caused this. Thanks to all who helped.