2

我收到此 c# 代码的错误

if (radioAll.Checked)
{
       SqlDataSource DataSource2 = new SqlDataSource();
       DataSource2.ID = "SqlDataSource2";
       this.Page.Controls.Add(DataSource2);
       DataSource2.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SEP_Project_NewConnectionString2"].ConnectionString;
       DataSource2.SelectCommand = "SELECT courseNo,title from Course";
       gridview_modules.DataSource = DataSource2;
       gridview_modules.DataBind();
}

错误如下 在此处输入图像描述

连接字符串没问题。如何修复此错误?

4

2 回答 2

1

你必须从你的SqlDataSource

gridview_modules.DataSource = DataSource2.Select(DataSourceSelectArguments.Empty);
gridview_modules.DataBind();
于 2013-07-11T08:08:49.767 回答
0

更改了gridview的列字段

之前是这样的,

<Columns>
  <asp:DynamicField HeaderText="Course No" />
  <asp:DynamicField HeaderText="Title" />
</Columns>

然后我变成了这样

<Columns>
 <asp:BoundField DataField="courseNo" HeaderText="Course No" />
 <asp:BoundField DataField="title" HeaderText="Title" />
</Columns>
于 2013-07-11T08:17:52.863 回答