我知道已经多次询问过这种类型的问题,并且已经解决了问题。我已经尝试了所有这些,但似乎都没有。请看看,也许可以帮助我理解我哪里出错了。
的.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上点击 LinkButton(commandName =“viewbat”)时,会更改多视图索引(gvCourses with datafield =“course”,并且工作正常)。
即使查询更改为简单,代码也会抛出相同的错误:
select * from tblBatches
数据库设计: