English 中文(简体)
表错误中不存在切除一栏原因。
原标题:Deleting column causes column does not exist in table error
  • 时间:2011-02-23 14:29:15
  •  标签:
  • c#
  • asp.net

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.

问题回答

d 我建议,在您第一次发言中,在BID中添加“BID”的内容。 或许这是它所期望的。

奥基先生,我写了很长的答复,但只是删除。 你们是否相信你会把正确的数据来源约束到你的电网?

Maybe:

GridView1.DataSource = _results.Tables["results"];

GridView1.DataSource = dt;

would w或k.

如果这是你电网的标志,我建议你修改电网,以具体显示你需要的栏目。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!