c# - MVC model out of range error -


i working on first mvc application , have run error.

i trying query database , show results on view page.

here code

 public class foreclosurelist {     public string area { get; set; }     public int numberoflistings { get; set; } } public class rets_listingsmodel {      public rets_listingsmodel(){} // empty constructor      public static ienumerable<foreclosurelist> getforeclosurelist() // making ienumerable list contain forclosure data     {         sqlconnection myconn;         sqlcommand mycmd;         sqldatareader myreader;              system.collections.arraylist aforclosurelist = new system.collections.arraylist(); // create array hold data, later converted ienumerable list.              string mysql =              "select [area], count (*) numberlistings listingtable" +              " foreclosureyn = 'y'" +              " , area <> ''" +              " group area";              myconn = new sqlconnection(configurationmanager.appsettings["connectionstring"]);             mycmd = myconn.createcommand();             mycmd.commandtext = mysql;             myconn.open();              foreclosurelist currentlist = new foreclosurelist(); // making instance foreclosurelist class , adding results query.              myreader = mycmd.executereader();             while (myreader.read())             {                  currentlist.area = (string)myreader["area"];                 currentlist.numberoflistings = (int)myreader["numberoflistings"];                 aforclosurelist.add(currentlist); // adding class object array             }               myreader.close();             myconn.close();              ienumerable<foreclosurelist> iforeclosurelist = aforclosurelist.cast<foreclosurelist>(); //converting array ienumerable list             return iforeclosurelist;         }      } 

but getting error saying: system.indexoutofrangeexception: numberoflistings

also on view page, in order access data correct in using following code on line 1

<%@ page language="c#" masterpagefile="~/views/shared/site.master" inherits="system.web.mvc.viewpage<foreclosure.models.foreclosurelist>" %> 

this problem:

string mysql = "select [area], count (*) numberlistings listingtable"   ... currentlist.numberoflistings = (int)myreader["numberoflistings"]; 

you don't fetch called numberoflistings. fetch numberlistings. they're not same.

additionally, should use using statements connection, statement , reader, change code follow .net naming conventions, stop using arraylist (favouring list<t> instead) , potentially move linq instead of manual sql (which have avoided error).


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -