asp.net遇到的问题,,,求助

dogaazz 2014-06-07 05:47:28
编译器错误消息: CS1502: 与“System.Data.Common.DataAdapter.Fill(System.Data.DataTable, System.Data.IDataReader)”最匹配的重载方法具有一些无效参数

源错误:



行 49: con.Open();
行 50: com = new System.Data.OleDb.OleDbCommand(strSql, con);
行 51: System.Data.Common.DataAdapter.Fill(myds, ddl.SelectedItem.Text);
行 52: this.DataGrid1.DataSource = myds.Tables[ddl.SelectedItem.Text].DefaultView;
行 53: this.DataGrid1.DataBind();


源文件: e:\Archive\WebSite1\teacher2.aspx.cs 行: 51





public partial class teacher2 : System.Web.UI.Page
{
protected System.Data.OleDb.OleDbCommand com;
protected System.Data.OleDb.OleDbDataReader reader;
protected System.Data.OleDb.OleDbConnection con;
protected System.Data.OleDb.OleDbDataAdapter da;
protected System.Data.DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("st.mdb") + ";User Id=admin;Password=;";
con = new OleDbConnection(strcon);
string id, strSql;
id = Session["admin"].ToString();
if (!IsPostBack)
{
strSql = "select xx from [class] where Tnumber='" + id + "'";
con.Open();
com = new System.Data.OleDb.OleDbCommand(strSql, con);
reader = com.ExecuteReader();
while (reader.Read())
{
ddl.Items.Add(reader.GetString(0));
}
reader.Close();
com.Dispose();
con.Close();
}
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet myds = new DataSet();
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("st.mdb") + ";User Id=admin;Password=;";
con = new OleDbConnection(strcon);
string strSql;
string sqlstr = "select sID,sName from "+ddl.SelectedItem.Text;
con.Open();
com = new System.Data.OleDb.OleDbCommand(strSql, con);
System.Data.Common.DataAdapter.Fill(myds, ddl.SelectedItem.Text);
this.DataGrid1.DataSource = myds.Tables[ddl.SelectedItem.Text].DefaultView;
this.DataGrid1.DataBind();
con.Close();

}
不知道该怎么改。。。问题出在DropDownList1这串代码里,我从上一界面进入这一页面的所以 Session["admin"]是有值的,问题出在DropDownList1这串代码里,这串代码里可能还有错能帮我改下吗谢谢
我本意是从DropDownList中选择课程,然后我的课程的表名(里面存储了这课的学生的名字和学号)就是DropDownList中的选项,然后选择了哪个表,我的DataGrid1中就显示出这个课程的学生的学号和名字
...全文
109 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
努力的阿牛 2014-06-07
  • 打赏
  • 举报
回复
我们转到System.Data.Common.DataAdapter这个类看一下就知道了,它的Fill方法中受保护的虚方法: protected virtual int Fill(DataTable dataTable, IDataReader dataReader); 所以这样用是错误的:System.Data.Common.DataAdapter.Fill(myds, ddl.SelectedItem.Text) 你应该用System.Data.OleDb下的OleDbDataAdapter。 实例化一个新的OleDbDataAdapter实例,用它填充DataSet;
save4me 2014-06-07
  • 打赏
  • 举报
回复
错误信息里面说明了System.Data.Common.DataAdapter.Fill没有(DataSet, String)的重载,换用DbDataAdapter.Fill 试一下下面的代码

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet myds = new DataSet();
        string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("st.mdb") + ";User Id=admin;Password=;";
        con = new OleDbConnection(strcon);
        string  strSql;
        string sqlstr = "select sID,sName from "+ddl.SelectedItem.Text;
        con.Open();
        com = new System.Data.OleDb.OleDbCommand(strSql, con);
        OleDbDataAdapter adapter = new OleDbDataAdapter(com)
        adapter .Fill(myds, ddl.SelectedItem.Text);
        this.DataGrid1.DataSource = myds.Tables[ddl.SelectedItem.Text].DefaultView;
        this.DataGrid1.DataBind();
        con.Close();
    }

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧