我正在尝试从gridview选择的索引更改事件中填充一个详细信息视图,但是我收到了错误,请帮助我
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);
}
答案 0 :(得分:0)
您正在尝试在需要集合或IEnumerable
试试这个
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;
}