2015-09-18 64 views
0

我收到以下錯誤。使用LINQ到別名的DataSet查詢填充Gridview

在選定的 數據源中找不到名稱爲「P_ID」的字段或屬性。

我想用LINQ綁定一個GridviewDataSet。我有三張桌子,我想要他們的ID's。在查詢中,我使用別名,但它給了我錯誤,因爲沒有找到別名。

這是我的代碼

string filterSO = "SELECT " + 
         "P.ID AS P_ID, " + 
         "S.ID AS S_ID, " +        
         " RS.LASTNAMER | | ' ' | | RS.FIRSTNAMER AS ReferentName, " + 
         " RS.ID," + 
         " P.STATUSP" + 
         " FROM PLANNING P," + 
         " SHIPPING S," +        
         " REFERENT_SHIPPING RS" +        
         " WHERE S.ID_REFERENT = RS.ID(+)" + 
         " AND S.ID_PLANNING = P.ID" + 
         " ORDER BY P.ID DESC"; 
    using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString())) 
    { 
     con.Open(); 
     OracleCommand cmd = new OracleCommand(filterSO, con); 
     OracleDataAdapter da = new OracleDataAdapter(cmd); 
     DataSet dss = new DataSet(); 
     da.Fill(dss, "office_all"); 
     Session["DATASET"] = dss; 

     var officee_all = from xx in dss.Tables["office_all"].AsEnumerable() 
         select new guards 
         { 
          ID = Convert.ToInt32(xx["P_ID"]), 
          ID_S = Convert.ToInt32(xx["S_ID"]),       

          LASTNAME_R = xx["LASTNAMER"].ToString(), 
          FIRSTNAME_R = xx["ReferentName"].ToString(), 
          ID_R = Convert.ToInt32(xx["ID"]), 
          STATUSP = xx["STATUSP"].ToString() 
         }; 

     GridViewSOFirst.DataSource = officee_all.ToList(); 
     GridViewSOFirst.DataBind(); 
+0

你準確得出哪個錯誤? – Sybren

+0

在所選數據源上找不到名稱爲「P_ID」的字段或屬性。 –

回答

0

字段或屬性具有名稱「P_ID」所選 數據源

該消息是不言自明上沒有被發現,這屬性在您使用的類中不可用,但顯然您嘗試綁定到此屬性。

由於您在sql查詢中選擇此列,但將其分配給屬性ID,您應該將其綁定到該屬性。

ID = Convert.ToInt32(xx["P_ID"]) 

Side-Note:您可以使用逐字字符串文字,它使得在C#中編寫SQL查詢變得更容易。我也會建議使用真正的/ ansi連接。即使「Oracle建議您使用FROM子句OUTER JOIN語法而不是Oracle加入運算符」link

string filterSO = @"SELECT 
        P.ID AS P_ID, 
        S.ID AS S_ID, 
        RS.LASTNAMER | | ' ' | | RS.FIRSTNAMER AS ReferentName, 
        RS.ID, 
        P.STATUSP 
        FROM PLANNING P, 
        SHIPPING S, 
        REFERENT_SHIPPING RS 
        WHERE S.ID_REFERENT = RS.ID(+) 
        AND S.ID_PLANNING = P.ID 
        ORDER BY P.ID DESC";