Search code examples
c#asp.netlinqlinq-to-dataset

Fill Gridview using LINQ to DataSet query with alias


I'm getting following error.

A field or property with the name 'P_ID' was not found on the selected data source.

I want to bind a Gridview using LINQ to DataSet. I have three tables from which I want theirs ID's. In query, I used aliases but it gives me error because the aliases are not found.

this is my code

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();

Solution

  • A field or property with the name 'P_ID' was not found on the selected data source

    The message is self-explanatory, this property is not available in the class that you have used, but apparently you are trying to bind to this property.

    Since you are selecting this column in the sql query but assign it to the property ID you should bind to that property instead.

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

    Side-Note: you could use a verbatim string literal, it makes it much easier to write a sql query in C#. I would also suggest to use real/ansi joins. Even "Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator" 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";