0

Out of the 4 tables that I have, 3 contain similar(client,policyno,policytype..) fields but with different data. The other table, however, has very different fields(client,sex,telephone,dob..). I have a search box in my view that makes use of the telephone number to display related records from all the 3 tables. Unfortunately, I am not able to handle the fields of the fourth table. Keeps returning the error

System.IndexOutOfRangeException: 'PolicyNo'

My Model:

    public class TableModels
    {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string Client { get; set; }
    public string PolicyNo { get; set; }
    public short? PolicyType { get; set; }
    public string Telephone { get; set; }

    //Health
    public DateTime? DOB { get; set; }

    public string Sex { get; set; }
    public string DepMemberNumber { get; set; }




       protected TableModels ReadValue(SqlDataReader reader)
    {
        TableModels obj = new TableModels();

        if (reader["ID"] != DBNull.Value)
        {
            obj.ID = (int)reader["ID"];
        }

        if (reader["Client"] != DBNull.Value)
        {
            obj.Client = (string)reader["Client"];
        }

        if (reader["PolicyNo"] != DBNull.Value)
        {
            obj.PolicyNo = (string)reader["PolicyNo"];
        }

        if (reader["PolicyType"] != DBNull.Value)
        {
            obj.PolicyType = (short)reader["PolicyType"];
        }

The error is returned on the Protected TableModel class.

Any advice would be appreciated

View:

@model IEnumerable

           @foreach (var item in Model.OrderByDescending(m => m.Client))
                {

                    <tr>
                        <td>
                            @Html.DisplayFor(modelitem => item.Client)
                        </td>

                        <td>
                            @Html.DisplayFor(modelitem => item.Telephone)
                        </td>
                        <td>
                            @Html.ActionLink("Details", "Details", new { id = item.Telephone })
                        </td>
                    </tr>
                }

Controller:

    public ActionResult Details(string id)
    {
        using (MainContext db = new MainContext())
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
            }

            List<SingleView> x1 = db.SingleViews.Where(a => a.Telephone == id).ToList();
            List<SingleViewM> x2 = db.SingleViewMs.Where(a => a.Telephone == id).ToList();
            List<SingleViewWst> x3 = db.SingleViewWsts.Where(a => a.Telephone == id).ToList();
            List<PensionsView> x4 = db.PensionsViews.Where(a => a.Telephone == id).ToList();
            List<Health> x5 = db.Health.Where(a => a.Telephone == id).ToList();

            SingleModel objview = new SingleModel();
            objview.USSD = x1;
            objview.Mombasa = x2;
Nickson
  • 15
  • 1
  • 5

0 Answers0