C# 实例化SqlDataSource时出错

C# 实例化SqlDataSource时出错,c#,asp.net,C#,Asp.net,我正在使用ASP.NET和C#在Visual Studio 2010中创建一个网站。我看到过一些例子,其中其他人将存储在数据库中的内容转换成一个列表,以便更好地使用它。但是,我选择将SqlDataSource附加到复选框列表中,并且在我的C#代码中使用SqlDataSource,以便查询数据库。这给了我一个错误: protected void BrandList_SelectedIndexChanged(object sender, EventArgs e) { //Connec

我正在使用ASP.NET和C#在Visual Studio 2010中创建一个网站。我看到过一些例子,其中其他人将存储在数据库中的内容转换成一个列表,以便更好地使用它。但是,我选择将SqlDataSource附加到复选框列表中,并且在我的C#代码中使用SqlDataSource,以便查询数据库。这给了我一个错误:

 protected void BrandList_SelectedIndexChanged(object sender, EventArgs e)
 {
      //Connecting to the database: Error here
      using (SqlDataSource ds = new SqlDataSource(ProductNameDS)) 

      //Loop through the checkbox
      foreach (ListItem product in BrandList.Items)
      {

           //If product is selected, return that item from the database to the
           //user

           if (product.Selected.Equals(Toshiba)
           {
                return (from c in context.Products
                     where c.Name Like 'Toshiba%'
                     select c.Name); 
           }

我得到的SqlDataSource错误是一个重载错误。如何修复此问题并连接到数据源?

如您所述,构造函数不带1个参数,请参阅下面链接中的文档。它需要一个连接字符串和一个select命令


您能公布实际错误吗?问题似乎是“SqlDataSource”接受3个或2个参数,或者不接受任何参数,但您只传递了1个参数。请看下面的图片。也可以考虑使用< <代码>之后的括号使用语句,以便更好的可读性。<代码> DS < /代码>与下面的代码有关?代码不应该以某种方式涉及上下文吗?错误是“System.Web.UI.WebControls.SqlDataSource”不包含接受1个参数的构造函数。我还应该提到,“ProductNameDS”参数是我附加到复选框列表的SqlDataSource的名称。