2015-09-28 71 views
0

我试图做实体之间的关系使用ADO。使用“新”关键字

public class Banner 
{ 
    public int IdBanner { get; set; } 
    public string NameBanner { get; set; } 
    public string Media { get; set; } 

    public Country Country{ get; set; } 
} 

public class Country 
{ 
    public int IdCountry { get; set; } 
    public string NameCountry { get; set; } 
} 

在我的另一个类(DAL),我有一个方法,这方法我需要插入国家类中的属性,如我为例:

public List<Banner> Listar() 
{ 
      List<Banner> lista = new List<Banner>(); 

      using (SqlConnection conn = ConnectionDAL.GetConnection()) 
      { 
       String sql = "BannerListar"; 

       using (SqlCommand command = new SqlCommand(sql, conn)) 
       { 

        command.CommandType = CommandType.StoredProcedure; 

        try 
        { 
         conn.Open(); 

         SqlDataReader dr = command.ExecuteReader(); 

         while (dr.Read()) 
         { 
          Banner obj = new Banner(); 

          if (dr["IdBanner"] != DBNull.Value) 
           obj.IdBanner = Convert.ToInt32(dr["IdBanner"]); 

          if (dr["NameBanner"] != DBNull.Value) 
           obj.NameBanner = dr["NameBanner"].ToString(); 

          if (dr["Media"] != DBNull.Value) 
           obj.Media = dr["Media"].ToString(); 


       //HERE the problem 
          if (dr["NameCountry"] != DBNull.Value) 
           obj.Country.NameCountry = dr["NameCountry"].ToString(); 



          lista.Add(obj); 

         } 
        } 
        catch 
        { 
         throw; 
        } 

        finally 
        { 
         conn.Close(); 
         conn.Dispose(); 
        } 


        return lista; 

       } 

      } 

     } 

当我做这个节目来我是这样的错误:“使用'新'关键字来创建一个对象实例” 我该如何解决它?

+0

你在哪一行得到错误? – Adil

+0

Here: if(dr [“NameCountry”]!= DBNull.Value) obj.Country.NameCountry = dr [“NameCountry”]。ToString(); – user1887732

+0

发生错误时。错误主要是因为您正在尝试创建某个类的实例而没有新的关键字。 – Dnyanesh

回答

1

您已经创建Banner类的对象,而是包含在Country类的旗帜对象。所以在使用它之前创建它。您可以在Banner类的默认构造函数中执行此操作。

public class Banner 
{ 
    public Banner() //Add the default constructor and initiate the Country object 
    { 
     Country = new Country(); 
    } 
    public int IdBanner { get; set; } 
    public string NameBanner { get; set; } 
    public string Media { get; set; } 

    public Country Country{ get; set; } 
} 

我已经给可能出现的问题的原因,并建议将获得以最小的努力摆脱错误的,故意保持简单为了更好地理解一个解决方案。您可以通过显式传递国家或传递null来代替国家或创建Country对象并将其分配给Banner中的对象。

+0

虽然看起来很干净,但是横幅没有与之关联的国家(比如表格列Banner.IdCountry被允许在SQL中接受NULL)。这仍然会给一个有效的国家,而不是不存在的国家来代表NULL数据库。 – niksofteng

+1

我用你的解决方案!韩国社交协会! – user1887732

+0

@vnikhil谢谢,我指出了错误的可能原因,并提出了摆脱错误的方法,以使问题简单化和重点突出。我通过建议OP按照要求调整来更新答案。 – Adil

3

你有intializing NameCountry

if (dr["NameCountry"] != DBNull.Value) 
    { 
     Country country = new Country(); 
     country.NameCountry = dr["NameCountry"].ToString(); 
obj.Country = country; 
} 
+0

正常工作,tks – user1887732

0

横幅类国家之前创建国家一流的对象是类属性为国类。因此,如果您在此属性中首先添加值,则需要为该类创建对象。

添加下面一行: - 没有被创建

obj.Country = new Country(); 
if (dr["NameCountry"] != DBNull.Value) 
    obj.Country.NameCountry = dr["NameCountry"].ToString();