1

In Asp.net C#,

I want to pass SQL View fields to object references where they are inside a nested class models. Because I need fields from two foreign Key related tables.

Nested Class :

public class BrandIconModel
{
 public Brand BrandViewModel { get; set; }
 public BrandIcon IconViewModel { get; set; }
}

inner Class 1:

public class Brand{
 public int ID{ get; set;}
 public string BrandName{ get; set;}
}

inner Class 2:

public class BrandIcon{
 public int ID{ get; set;}
 public string Icon{ get; set;}
 public int BrandID{ get; set;}
}

public List<BrandIconModel> GetBrands()
{
     connection();
     SqlCommand cmd = new SqlCommand("getBrands", con);
     cmd.CommandType = CommandType.StoredProcedure;
     List<BrandIconModel> l_bim = new List<BrandIconModel>();
     con.Open();
     SqlDataReader sdr = cmd.ExecuteReader();
     while (sdr.Read())
     {
      BrandIconModel biv = new BrandIconModel();
      biv.BrandViewModel.ID = Convert.ToInt32(sdr["ID"]);             
      biv.BrandViewModel.BrandName = sdr["BrandName"].ToString();
      biv.IconViewModel.Icon = sdr["BrandIcon"].ToString();     
      l_biv.Add(biv);
   }
   con.Close();
   return l_biv;

}

I cannot reach BrandIconModel's inner class properties in GetBrands(). Gives "Object reference is not set to instance of object" exception.

How can I reach inner class properties? Thanks

Zeynep
  • 159
  • 1
  • 15

0 Answers0