Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 138:                //    a equals p.Attribute("first_name").Value[0] into groups
Line 139:                join p in xml.Descendants("employee") on
Line 140:                    a equals p.Attribute("first_name").Value[0] into groups
Line 141:                orderby a
Line 142:                select new

my Code was:

protected void Selecting(object sender, LinqDataSourceSelectEventArgs args)
        {
            //  load the document
            //XDocument xml = XDocument.Load(this.Server.MapPath(@"app_data\employees.xml"));
            EffOneUser user = new EffOneUser();
            DataTable dt = user.GetUserDt(null, USER_STATUS, null);

            DataSet ds = new DataSet();
            ds.DataSetName = "employees";
            ds.Tables.Add(dt);
            ds.Tables[0].TableName = "employee";

            XDocument xml = XDocument.Parse(ds.GetXml());
            //  define the alphabet
            char[] alphabet = 
            {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
                'Y', 'Z' 
            };

            //  use linq to extract the elements we are interested in
            args.Result =
                from a in alphabet
                join p in xml.Descendants("employee").Descendants("first_name") on
                    a equals p.Element("first_name").Value[0] into groups
                orderby a
                select new
                {
                    Key = a,
                    Count = groups.Count(),
                    Employees =
                        from e in groups
                        orderby e.Element("first_name").Value[0]
                        select new
                        {
                            FirstName = e.Element("first_name").Value[0],
                            LastName = e.Element("last_name").Value[0],
                            //Title = e.Attribute("title").Value,
                            EmailAddress = e.Element("email_addr").Value[0],
                            Phone = e.Element("phone").Value[0]
                        }
                };
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.