问题
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