请高手帮忙
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
找了半天不知道那里的错误
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
找了半天不知道那里的错误
解决方案 »
- 图片自动切换的效果,上面的数字导航是用flash做的吗?
- Gridview B/S 导出到Excel 会讲的来~会翻译的GO GO GO!
- 150分求组个关于打印的问题
- Menu控件------------------请大家帮忙
- 大家有没有遇到过这要的问题: 无法在Web服务器上启动调试,未将项目配置为进行调试.
- 高手帮忙 问个ASP。NET弱弱问题
- 遍历treeview,如何得到ID?
- iis不能发布.aspx的页面为什么!!!!!!!!!!!
- ASP.NET页面运行原理的问题~~
- 关于aspx登录失败...
- 怎么把VS2003+SQL2000的应用程序转换为VS2008+SQL2000的应用程序??
- 用户 'sa' 登录失败,请求帮助
换成
System.Data.DataSet
//应为
SqlCommand command = new SqlCommand("Select distinct, sex from UserInfo", connection);
为:
System.Data.DataSet
为:
System.Data.DataSet
{
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 引用的类对不对,试试吧
能编译通过?不可思议。
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,connection);