Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource

人走茶凉 提交于 2019-12-13 04:28:47

问题


i am trying to fill one details view from the gridview selected index changing event,but i got an error,plz help me

public void viewDetailsNew(decimal decDv)
    {
        SubjectInfo infosubject = new SubjectInfo();
        SubjectSp spcubject = new SubjectSp();
        dvSubject.DataSource = spcubject.SubjectViewDetails(decDv);
        dvSubject.DataBind();
        mvSubject.ActiveViewIndex = 1;
    }

public SubjectInfo SubjectViewDetails(decimal decsubjectid)
    {
        SubjectInfo infosubject = new SubjectInfo();
        SqlDataReader sqlreader = null;
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlCommand sqlcmd = new SqlCommand("SubjectView", sqlcon);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Parameters.Add("@subjectId", SqlDbType.Decimal).Value = decsubjectid;
            sqlreader = sqlcmd.ExecuteReader();
            while (sqlreader.Read())
            {
                infosubject.subjectId = decimal.Parse(sqlreader["subjectId"].ToString());
                infosubject.subjectName = sqlreader["subjectName"].ToString();
                infosubject.shortName = sqlreader["shortName"].ToString();

            }
        }
        catch
        {
            throw;
        }
        finally
        {
            sqlreader.Close();
            sqlcon.Close();
        }

        return infosubject;
    }

protected void gvViewSubject_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        ViewState["subjectId"] = gvViewSubject.DataKeys[e.NewSelectedIndex]["subjectId"].ToString();
        decimal decDv = decimal.Parse(ViewState["subjectId"].ToString());
        viewDetailsNew(decDv);
    }

回答1:


You are trying to bind to a single object when you need a collection or IEnumerable

Try this

 public IEnumerable<SubjectInfo> SubjectViewDetails(decimal decsubjectid)
{
    var list = new List<SubjectInfo>();

    try
    {
        if (sqlcon.State == ConnectionState.Closed)
        {
            sqlcon.Open();
        }
        SqlCommand sqlcmd = new SqlCommand("SubjectView", sqlcon);
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.Parameters.Add("@subjectId", SqlDbType.Decimal).Value = decsubjectid;
        using (var sqlreader = sqlcmd.ExecuteReader())
        {
            while (sqlreader.Read())
            {
                SubjectInfo infosubject = new SubjectInfo();
                infosubject.subjectId = decimal.Parse(sqlreader["subjectId"].ToString());
                infosubject.subjectName = sqlreader["subjectName"].ToString();
                infosubject.shortName = sqlreader["shortName"].ToString();
                list.Add(infosubject);
            }
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        sqlcon.Close();
    }

    return list;
}


来源:https://stackoverflow.com/questions/16535023/data-source-is-an-invalid-type-it-must-be-either-an-ilistsource-ienumerable-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!