请高手帮忙  
    private void BindSex()
    {
        SqlConnection connection = new SqlConnection("Data Source=localhost; Initial Catalog=AspNetStudy; Persist Secrity Info=true;User ID=sa;Password=123123");
        SqlCommand command = new SqlCommand("Select distinct sex from UserInfo", connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataTable data = new DataTable();
        adapter.Fill(data);        DataList1.DataSource = data;
        DataList1.DataBind();
    }    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lbSex = (Label)(e.Item.FindControl("Label1"));
            DataList dl2 = (DataList)(e.Item.FindControl("DataList2"));            bool male = bool.Parse(lbSex.Text);
            dl2.DataSource = GetDataTable(male);
            dl2.DataBind();
        }
    }    private DataTable GetDataTable(bool male)
    {
        SqlConnection connection = new SqlConnection("Data Source=localhost; Initial Catalog=AspNetStudy; Persist Secrity Info=true;User ID=sa;Password=123123");
        SqlCommand command = new SqlCommand("select top 3 RealName from UserInfo where sex@sex order by UserID", connection);
        command.Parameters.AddWithValue("@Sex", male);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataTable data = new DataTable();
        adapter.Fill(data);
        return data;
    }
以上是源代码,编译时总出现错误:
错误1:与“System.Data.Common.DbDataAdapter.Fill(System.Data.DataTable)”最匹配的重载方法具有一些无效参数D:\My Documents\Visual Studio 2008\WebSites\WebSite1\DataListDemo.aspx.cs 32
错误2:参数“1”: 无法从“DataTable”转换为“System.Data.DataTable” D:\My Documents\Visual Studio 2008\WebSites\WebSite1\DataListDemo.aspx.cs
找了半天不知道那里的错误

解决方案 »

  1.   

    添加System.Data引用了吗?无法从DataTable转换为System.Data.DataTable,说明你的DATATABLE的类型有问题
      

  2.   

    System.Data.Common.DbDataAdapter.Fill(System.Data.DataTable)
    换成
    System.Data.DataSet
      

  3.   


    //应为
      SqlCommand command = new SqlCommand("Select distinct, sex from UserInfo", connection);
      

  4.   

    System.Data.DataTable
    为:
    System.Data.DataSet
      

  5.   

    System.Data.DataTable
    为:
    System.Data.DataSet
      

  6.   

    上面的方法改为下面代码试试 private void BindSex()
      {
      SqlConnection connection = new SqlConnection("Data Source=localhost; Initial Catalog=AspNetStudy; Persist Secrity Info=true;User ID=sa;Password=123123");
       //这行去掉试试,直接用sqlDataAdapter就好
      //SqlCommand command = new SqlCommand("Select distinct sex from UserInfo", connection);
      SqlDataAdapter adapter = new SqlDataAdapter("Select distinct sex from UserInfo", connection);
      DataTable data = new DataTable();
      adapter.Fill(data);
      DataList1.DataSource = data;
      DataList1.DataBind();
      }
    再看看你的DataTable 引用的类对不对,试试吧
      

  7.   

    支持,印象中Fill里面只能放DataSet。
    能编译通过?不可思议。
      

  8.   

    System.Data 已经引用了。而且换成DataSet也无法编译。到底是哪出了错误??这代码还是书上的源码,是不是软件的问题?这个我得引用
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
      

  9.   

     SqlDataAdapter adapter = new SqlDataAdapter(command);
    改成 SqlDataAdapter adapter = new SqlDataAdapter(command,connection);