在所选数据源上找不到名为“bname”的字段或属性

时间:2018-06-08 18:49:01

标签: asp.net sql-server gridview templatefield

我知道已经多次询问过这种类型的问题,并且已经解决了问题。我已经尝试了所有这些,但似乎都没有。请看看,也许可以帮助我理解我哪里出错了。

的.aspx

      <asp:view ID="view2" runat="server">
            <asp:GridView ID="gvBatches" runat="server" AutoGenerateColumns="False" CssClass="table-hover table" GridLines="None" Width="900px" ShowFooter="True" >
            <columns>
               <asp:BoundField  DataField="bname" HeaderText="Batch Name" />
            </columns>  
            </asp:GridView>
  </asp:view>

.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
         {

            if (!Page.IsPostBack)
            {
                if (ViewState["query"] == null)
                {
                    ViewState["query"] = "select course from tblCourses";
                }
                if (MultiView1.ActiveViewIndex == '1')
                {
                    bindgrid1();
                }

                bindgrid();
            }
        }

    protected void bindgrid1()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
        DataTable dt1 = new DataTable();
        using (SqlDataAdapter sda = new SqlDataAdapter(ViewState["query"].ToString(), con))
        {
            sda.Fill(dt1);
        }
        gvBatches.DataSource = dt1;
        gvBatches.DataBind();
    }
if (e.CommandName == "viewbat")
        {
            ViewState["query"] = "select bname from tblBatches where course='" + e.CommandArgument.ToString() + "'";
            MultiView1.ActiveViewIndex = 1;
        }

在不同的GridView上点击 LinkBut​​ton(commandName =“viewbat”)时,会更改多视图索引(gvCourses with datafield =“course”,并且工作正常)。

即使查询更改为简单,代码也会抛出相同的错误:

select * from tblBatches

数据库设计:

Database design is as shown, consists 'bname'

0 个答案:

没有答案