1

I have been playing around with linking XML files to dropdown lists and gridviews.

I have managed to populate one dropdown list from the XML document and then a gridview to another but when try to add a where clause, I get a null reference exception and not sure why. How can I resolve this?

XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));
var q = from c in xmlDoc.Descendants("Images")
        where c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString()
        select new
        {
            PropertyID = c.Element("ThumbUrl").Value,
        };
GridView1.DataSource = q;
GridView1.DataBind();
3
  • Is nullreference thrown at where part? Are you sure "PropertyId" attribute exists on "Images" node...? Commented Apr 22, 2011 at 8:05
  • ok thanks for that managed to get it working to a point so much appreciated - one last qestion if you can help....hte gridview now shows the correct ThumbUrl when dropdown box changes - but how do i change the gridview to link to the image as opposed to just showing the url? Commented Apr 22, 2011 at 9:15
  • Kev - to get the best out of StackOverflow you need to vote-up / accept answers that help you, and you stand a much better chance of getting an answer to your second question if you post it in its own right rather than in a comment. Commented Apr 26, 2011 at 8:06

4 Answers 4

6

Avoid using .Value; a range of null-safe implicit conversion operators are available:

var q = from c in xmlDoc.Descendants("Images")
        where (string)c.Attribute("PropertyId")
               == DropDownList1.SelectedValue.ToString()
        select new
        {
            PropertyID = (string)c.Element("ThumbUrl"),
        };
Sign up to request clarification or add additional context in comments.

1 Comment

This deserves more recognition
1

Any of these:

c.Attribute("PropertyId")
c.Element("ThumbUrl")
DropDownList1.SelectedValue

could be null, and then calling .ToString() or .Value on them would give you the exception you're seeing.

If you're not happy to catch XML problems via NullReferenceExceptions, then you need to take the value of the Attribute() call into a local variable and then test against that (or call it twice and test the first call for null).

Comments

0

Try: where c.Attribute("PropertyId") != null && c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString() for the condition part and c.Element("ThumbUrl") != null . Your code should look like this:

XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));
var q = from c in xmlDoc.Descendants("Images")
        where c.Attribute("PropertyId") != null 
        && c.Attribute("PropertyId").Value == DropDownList1.SelectedValue.ToString() 
        && c.Element("ThumbUrl") != null
        select new
        {
            PropertyID = c.Element("ThumbUrl").Value,
        };
GridView1.DataSource = q;
GridView1.DataBind();

Comments

0
from x in document.Descendants("Images")
let xElement = x.Element("PropertyId")
where xElement != null && xElement.Value == DropDownList1.SelectedValue.ToString()
select new
{
   PropertyID = c.Element("ThumbUrl").Value,
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.