2013-05-15 90 views
1

目前正在修復我的代碼,我添加了一個搜索功能 ..但是當我調試它時,出現了一些錯誤,並且出現錯誤。MVC3 ASP.NET C#System.InvalidOperationException:當數據不存在時無效嘗試讀取。

這裏是我的代碼:

public ViewResult Index(string currentFilter, string searchString, int? page) { 
    if(Request.HttpMethod=="GET") { 
     searchString=currentFilter; 
    } 
    else { 
     page=0; 
    } 

    ViewBag.CurrentFilter=searchString; 

    var connString=ConfigurationManager.ConnectionStrings["ApplicantDB"].ConnectionString; 
    List<Applicant> instructors=new List<Applicant>(); 

    using(var conn=new SqlConnection(connString)) { 
     conn.Open(); 

     var query=new SqlCommand(
      "SELECT TOP 50 APPLICANT_ID, APPLICANT_Lastname, APPLICANT_FirstName, APPLICANT_MiddleName, APPLICANT_Address, APPLICANT_City"+ 
      " FROM APPLICANT", conn); 

     var reader=query.ExecuteReader(); 

     int currentPersonID=0; 
     Applicant currentInstructor=null; 

     while(reader.Read()) { 
      var personID=Convert.ToInt32(reader["APPLICANT_ID"]); 

      if(personID!=currentPersonID) { 
       currentPersonID=personID; 

       if(currentInstructor!=null) { 
        instructors.Add(currentInstructor); 
       } 

       currentInstructor=new Applicant(); 
       currentInstructor.APPLICANT_ID=Convert.ToInt32(reader["APPLICANT_ID"].ToString()); 
       currentInstructor.APPLICANT_Lastname=reader["APPLICANT_Lastname"].ToString(); 
       currentInstructor.APPLICANT_FirstName=reader["APPLICANT_FirstName"].ToString(); 
       currentInstructor.APPLICANT_MiddleName=reader["APPLICANT_MiddleName"].ToString(); 
       currentInstructor.APPLICANT_Address=reader["APPLICANT_Address"].ToString(); 
       currentInstructor.APPLICANT_City=reader["APPLICANT_City"].ToString(); 
      } 

     } 

     if(!String.IsNullOrEmpty(searchString)) { 
      currentInstructor= 
       instructors.Where(
        s => 
         s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) 
         || 
         s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper()) 
        ).FirstOrDefault(); 

      currentInstructor.APPLICANT_ID=Convert.ToInt32(reader["APPLICANT_ID"].ToString()); 
      currentInstructor.APPLICANT_Lastname=reader["APPLICANT_Lastname"].ToString(); 
      currentInstructor.APPLICANT_FirstName=reader["APPLICANT_FirstName"].ToString(); 
      currentInstructor.APPLICANT_MiddleName=reader["APPLICANT_MiddleName"].ToString(); 
      currentInstructor.APPLICANT_Address=reader["APPLICANT_Address"].ToString(); 
      currentInstructor.APPLICANT_City=reader["APPLICANT_City"].ToString(); 
     } 

     if(currentInstructor!=null) { 
      instructors.Add(currentInstructor); 
     } 

     reader.Close(); 
     conn.Close(); 
    } 

    int pageSize=10; 
    int pageNumber=(page??0); 
    return View(instructors.ToPagedList(pageNumber, pageSize)); 
} 

而且錯誤出現在這一行..說:

無效嘗試時不存在數據讀取。

描述:在當前web請求的執行過程中發生

未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:

System.InvalidOperationException:無效嘗試時不存在數據讀取。

currentInstructor= 
    instructors.Where(
     s => 
      s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) 
      || 
      s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper()) 
     ).FirstOrDefault(); 

currentInstructor.APPLICANT_ID=Convert.ToInt32(reader["APPLICANT_ID"].ToString()); 
currentInstructor.APPLICANT_Lastname=reader["APPLICANT_Lastname"].ToString(); 

回答

1

它應該有如下......

public ViewResult Index(string currentFilter, string searchString, int? page) 
{ 
    if (Request.HttpMethod == "GET") 
     searchString = currentFilter; 
    else 
     page = 0; 
    ViewBag.CurrentFilter = searchString;  
    var connString = ConfigurationManager.ConnectionStrings["ApplicantDB"].ConnectionString; 
    List<Applicant> instructors = new List<Applicant>(); 
    using (var conn = new SqlConnection(connString)) 
    { 
     conn.Open(); 
     var query = new SqlCommand("SELECT TOP 50 APPLICANT_ID, APPLICANT_Lastname, APPLICANT_FirstName, APPLICANT_MiddleName, APPLICANT_Address, APPLICANT_City" + 
       " FROM APPLICANT", conn); 
     int currentPersonID = 0; 
     Applicant currentInstructor = null; 

     using (var reader = query.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 
       var personID = Convert.ToInt32(reader["APPLICANT_ID"]); 
       if (personID != currentPersonID) 
       { 
        currentPersonID = personID; 
        if (currentInstructor != null) 
         instructors.Add(currentInstructor); 
        currentInstructor = new Applicant(); 
        currentInstructor.APPLICANT_ID = Convert.ToInt32(reader["APPLICANT_ID"].ToString()); 
        currentInstructor.APPLICANT_Lastname = reader["APPLICANT_Lastname"].ToString(); 
        currentInstructor.APPLICANT_FirstName = reader["APPLICANT_FirstName"].ToString(); 
        currentInstructor.APPLICANT_MiddleName = reader["APPLICANT_MiddleName"].ToString(); 
        currentInstructor.APPLICANT_Address = reader["APPLICANT_Address"].ToString(); 
        currentInstructor.APPLICANT_City = reader["APPLICANT_City"].ToString(); 
       } 
       if (!String.IsNullOrEmpty(searchString)) 
       { 
        currentInstructor = 
         instructors.Where(
          s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) || 
            s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())). 
          FirstOrDefault(); 
        currentInstructor.APPLICANT_ID = Convert.ToInt32(reader["APPLICANT_ID"].ToString()); 
        currentInstructor.APPLICANT_Lastname = reader["APPLICANT_Lastname"].ToString(); 
        currentInstructor.APPLICANT_FirstName = reader["APPLICANT_FirstName"].ToString(); 
        currentInstructor.APPLICANT_MiddleName = reader["APPLICANT_MiddleName"].ToString(); 
        currentInstructor.APPLICANT_Address = reader["APPLICANT_Address"].ToString(); 
        currentInstructor.APPLICANT_City = reader["APPLICANT_City"].ToString(); 
       } 
      } 
     } 
     if (currentInstructor != null) 
     { 
      instructors.Add(currentInstructor); 
     } 
    } 

    int pageSize = 10; 
    int pageNumber = (page ?? 0); 
    return View(instructors.ToPagedList(pageNumber, pageSize)); 
} 
+0

想你說更換代碼後。 。 我得到的NullReferenceException currentInstructor = instructors.Where( S => s.APPLICANT_Lastname.ToUpper()。包含(searchString.ToUpper())|| s.APPLICANT_FirstName.ToUpper()。包含(searchString.ToUpper( )))。 FirstOrDefault(); currentInstructor.APPLICANT_ID = Convert.ToInt32(reader [「APPLICANT_ID」]。ToString()); –

+0

我將如何將它包圍在循環中先生? –

相關問題