c# 3.0 - MVC3 ASP.NET C# System.InvalidOperationException: Invalid attempt to read when no data is present. -


currently fixing code , i'm adding search function .. when debug it, went wrong , error pops out.

here's code:

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"+             " 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)); } 

and error comes in line .. saying:

invalid attempt read when no data present.

description:

an unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.

exception details:

system.invalidoperationexception: invalid attempt read when no data present.

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(); 

it should below...

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" +                 " 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)); } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -