0

I am currently developing a WPF project in c#. The project takes a string (newMemoryRFID) which is defined when the page is initialised and uses it in a query. Like so

var query = 
            from c in MemoryData.Memory
                    where c.RFID == newMemoryRFID
                    select c;
        this.DataContext = query;
        this.View = ((CollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));

This produces an empty DataContext

However when I use test data which is the same as what newMemoryRFID would be the query i.e.

var query = 
            from c in MemoryData.Memory
                    where c.RFID == "0F02D76B05"
                    select c;
        this.DataContext = query;
        this.View = ((CollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));

The query gets the correct record. As you may be able to tell I'm not the best programmer so the simpler your answer the better. And thanks very much in advance

1
  • 1
    If it works when you hard-code the newMemoryRFID string, then the problem has to lie with newMemoryRFID. Put a breakpoint in your code before it executes. Then inspect the value of newMemoryRFID. Chances are there might be some white spaces or something in there. Commented Apr 15, 2011 at 9:22

2 Answers 2

3

This is the time to use your debugger. It sounds like newMemoryRFID isn't set to "0F02D76B05" at the time that query is created.

If you can't step into it, at least do

Debug.WriteLine(string.Format("newMemoryRFID = {0}", newMemoryRFID); 

before the line

var query = ...
Sign up to request clarification or add additional context in comments.

4 Comments

I wish it was that, but I've followed newMemoryRFID from start to finish and from what I can tell it matches up with what I'm expecting i.e. newMemoryRFID "\n0F02D76B05\r" string
Are you expecting newMemoryRFID to include the \n and \r? That looks like you've got extra characters. Try using newMemoryRFID.Trim().
I wasn't expecting them, but I've tried using .Trim() and it hasn't helped :(
This should write false to the debug trace (or you can check it in the immediate window or whatever). Debug.WriteLine(string.Format("newMemoryRFID == \"0F02D76B05\" is {0}", newMemoryRFID == "0F02D76B05"); Those extra non-printable characters mean that the two strings aren't the same. You should just need to clean up whatever you are getting from your source. .Replace("\n", "").Replace("\r", "") is the quick and oh-so-hacky way. Look into regular expressions or the communication specifications of your RFID reader (txt2re.com might help you out here).
0

Try trimming the string both at the beginning and end for possible whitespace which would fail the string match.

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.