2015-09-28 40 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();