0

我正在尝试从 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);
    }
4

1 回答 1

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;
}
于 2013-05-14T08:58:23.363 回答