locked
How to retrieve case record and display in gridview in asp.net RRS feed

  • Question

  • Hi,

    I have retrieved case record when I retrieve description value of case then error is occurred "the given key was not present in the dictionary". Please tell me how to resolve this issue

    Here is my code 

      QueryExpression query1 = new QueryExpression("incident");
                    query1.ColumnSet.AddColumns("title","description", "createdon");

                    query1.Criteria = new FilterExpression();
                  
                    query1.Criteria.AddCondition("customeridname", ConditionOperator.Equal, lblUser.Text);


                   
                    EntityCollection result = serviceProxy.RetrieveMultiple(query1);
                 
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Customer");
                    dt.Columns.Add("Title");
                 
                    dt.Columns.Add("Description");
                    dt.Columns.Add("Created On");

                    foreach (Entity entities in result.Entities)
                    {
                     
                        DataRow dr = dt.NewRow();
                        dr["Title"] = entities.Attributes["title"].ToString();
                        dr["Description"] = entities.Attributes["description"].ToString();
                        dr["Created On"] = entities.Attributes["createdon"].ToString();
                       dr["Customer"] = lblUser.Text;
                      
                        dt.Rows.Add(dr);


                    }

                    ds.Tables.Add(dt);

                    ordergrid.DataSource = ds;
                    ordergrid.DataBind();
               

    Monday, July 14, 2014 9:28 AM

All replies

  • Hi,

    Whenever there is no value in the record, we don't get the attribute for that record. You can write a one common method and pass Entity and required attribute value. So that it could return the value.

     public string GetValueFromEntity(Entity objEntity, string strAttriubtueName)
            {
                string strValue = string.Empty;
                if (objEntity.Attributes.Contains(strAttriubtueName) && objEntity.Attributes[strAttriubtueName] != null)
                {
                    strValue = objEntity.Attributes[strAttriubtueName].ToString();
                }
                return strValue;
            }

    you the above method in ForEach loop like below...

    dr["description"] = GetValueFromEntity(entities, "description");

    --

    Thanks and Regards,

    Gopinath.

    http://mscrmtechie.blogspot.com


    Monday, July 14, 2014 11:40 AM
  • Hi Aamir,

    This could probably be due to the fact there is no description present for the record(s). As a good programming practice, always check if the property bag contains your attribute and then try to retrieve it. Something like follows:

    dr["Description"] = entities.Attributes.Contains("description") ? entities.Attributes["description"].ToString() : string.Empty;


    Admin QuikView Solution for CRM 2013

    Monday, July 14, 2014 11:43 AM