BLL 、Model、IDAL、SqlServerDAL下面都有个文件夹School,school文件夹下面都有PraxisInfro.cs
aspx.cs代码:里面的PraxisId就是我的主键,自增的
          /*从PraxisInfro表中读取此用户的PraxisId,,此时取出后一次插入时主键值*/
        BLL.School.PraxisInfro bll1 = new BLL.School.PraxisInfro();
        Model.School.PraxisInfro model1 = bll1.GetLastid();
        PraxisId = model.PraxisId.ToString();BLL重的代码:
namespace BLL.School
{
   public class PraxisInfro
    {         public Model.School.PraxisInfro GetLastid()
       {
           return DALFactory.DataAccess.PraxisInfro.GetLastid();
       }
    }
}IDAL重的代码:namespace IDAL.School
{
    public interface IPraxisInfro
    {
        Model.School.PraxisInfro GetLastid();    }
}最关键的SqlServerDAL代码:
   public Model.School.PraxisInfro GetLastid()
        {            SqlDataReader dr = SqlServerHelper.ExecuteReader   (CommandType.StoredProcedure, "PraxisInfro_GetLastid");
            Model.School.PraxisInfro model = new Model.School.PraxisInfro();            while (dr.Read())
            {
              
                model.PraxisId= (int)dr["PraxisId"];
                model.Profession = dr["Profession"].ToString();
                model.Degree = dr["Degree"].ToString();
                model.ForeignLanguage = dr["ForeignLanguage"].ToString();
                model.MaleStudents = dr["MaleStudents"].ToString();
                model.FemaleStudents = dr["FemaleStudents"].ToString();
                model.WorkYear = dr["WorkYear"].ToString();
                model.WorkMonth = dr["WorkMonth"].ToString();
                model.TimeRange = dr["TimeRange"].ToString();
                model.Request = dr["Request"].ToString();
                model.Introduction = dr["Introduction"].ToString();
                                   }
            dr.Close();            return model;
        }
他怎么提示错误:
PraxisId 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: PraxisId源错误: 
行 110:            {
行 111:              
行 112:                model.PraxisId= (int)dr["PraxisId"];行 113:                model.Profession = dr["Profession"].ToString();
行 114:                model.Degree = dr["Degree"].ToString();
 

解决方案 »

  1.   

    还有我的存储过程 
    /*功能:取出最后插入的主键值 */
    CREATE Procedure [dbo].[PraxisInfro_GetLastid]AS 
    select IDENT_CURRENT(' PraxisInfro')
    GO
      

  2.   

    因为当出来的dr是null,再read就会报outofIndex的异常
    应该在
    while(dr.read())
    前加个判断
    if(dr!=null)
    {
      while(dr.read())
      {
        //your code
      }
    }
      

  3.   

    你确信你执行PraxisInfro_GetLastid存储过程后的查询结果中的列名是"PraxisId"吗?,如果列名写错了,会出现如你所示的错误提示
      

  4.   

    不好意思,刚才看到了你的存储过程,里面就只是返回一个int,可是你的reader最后怎么会读出 model.Profession = dr["Profession"].ToString(); 
                    model.Degree = dr["Degree"].ToString(); 
                    model.ForeignLanguage = dr["ForeignLanguage"].ToString(); 
                    model.MaleStudents = dr["MaleStudents"].ToString(); 
                    model.FemaleStudents = dr["FemaleStudents"].ToString(); 
                    model.WorkYear = dr["WorkYear"].ToString(); 
                    model.WorkMonth = dr["WorkMonth"].ToString(); 
                    model.TimeRange = dr["TimeRange"].ToString(); 
                    model.Request = dr["Request"].ToString(); 
                    model.Introduction = dr["Introduction"].ToString(); 这么多东西呢?肯定是读不出来的,你可以把这个注释掉以后再试试,或者注释掉第一句试试