高手求助!!!!!
请高手帮忙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 易做图 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 易做图@易做图 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
找了半天不知道那里的错误 --------------------编程问答-------------------- 添加System.Data引用了吗?
无法从DataTable转换为System.Data.DataTable,说明你的DATATABLE的类型有问题
--------------------编程问答-------------------- System.Data.Common.DbDataAdapter.Fill(System.Data.DataTable)
换成
System.Data.DataSet --------------------编程问答-------------------- 顶二楼 没有引用包 --------------------编程问答--------------------
--------------------编程问答-------------------- System.Data.DataTable
//应为
SqlCommand command = new SqlCommand("Select distinct, 易做图 from UserInfo", connection);
为:
System.Data.DataSet --------------------编程问答-------------------- System.Data.DataTable
为:
System.Data.DataSet --------------------编程问答-------------------- 最匹配的重载方法具有一些无效参数 --------------------编程问答-------------------- 上面的方法改为下面代码试试
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 易做图 from UserInfo", connection); SqlDataAdapter adapter = new SqlDataAdapter("Select distinct 易做图 from UserInfo", connection);
DataTable data = new DataTable();
adapter.Fill(data);
DataList1.DataSource = data;
DataList1.DataBind();
}
再看看你的DataTable 引用的类对不对,试试吧
--------------------编程问答--------------------
支持,印象中Fill里面只能放DataSet。
能编译通过?不可思议。 --------------------编程问答-------------------- 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;
--------------------编程问答-------------------- SqlDataAdapter adapter = new SqlDataAdapter(command);
改成
SqlDataAdapter adapter = new SqlDataAdapter(command,connection);
补充:.NET技术 , ASP.NET