7 Replies - 237 Views - Last Post: 19 March 2012 - 02:41 PM Rate Topic: -----

#1 chris99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 09-February 10

search problem with xml and linq

Posted 19 March 2012 - 12:31 AM

Hello.

I wanted to make an address book in C# and this is my first try with LINQ.
Generally it works O.K. but there is a strange problem:

When I search for an contact and I double click on the contact found in search instead of showing me the details for that contact it shows the details for the first contact in list or if I choose the second option from search it shows me the details for the second contact in list instead of showing me the details for the contact in search...(I use the same window for listing contacts and showing the search results)

Pictures and code:

Posted Image

Posted Image

Posted Image


Code for Search:

private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            //If enter key was pressed then search for that key in database and populate the list.
            if (e.KeyChar == (char)13)
            {

                string searhName = txtSearch.Text;
                List<Contacts> searchedContactList = new List<Contacts>();

                foreach (Contacts contact in XMLParse.ContactList)
                {
                    if (contact.FName.IndexOf(searhName, StringComparison.CurrentCultureIgnoreCase) != -1|| contact.LName.IndexOf(searhName, StringComparison.CurrentCultureIgnoreCase) != -1)
                        searchedContactList.Add(contact);
                }
               
                populateData(searchedContactList);

            }

        }



Code for populatedata method:

private void populateData(List<Contacts> ContactList)
        {            

            _dgAddressBook.BeginUpdate();
            _dgAddressBook.Items.Clear();

            foreach (Contacts cts in ContactList)
            {
                ListViewItem item = _dgAddressBook.Items.Add(cts.FName);
                item.SubItems.Add(cts.LName);
                item.SubItems.Add(cts.Phone1);
                item.SubItems.Add(cts.Phone2);
                item.SubItems.Add(cts.Address1);
                item.SubItems.Add(cts.Email);
            }

            _dgAddressBook.EndUpdate();
                                               
        }



Code for double click in list(search results window)

private void _dgAddressBook_DoubleClick(object sender, EventArgs e)
        {
            if (_dgAddressBook.SelectedItems.Count <= 0)
                return;

            int index = _dgAddressBook.SelectedItems[0].Index;
            AddressDetail ad = new AddressDetail(XMLParse.ContactList, index);
            ad.ShowDialog(); // details for contact form
            populateData(XMLParse.ContactList);

        }


Code for Contact list

public static List<Contacts> ContactList
        {
            get
            {
                if (_contactList == null)
                    _contactList = new List<Contacts>();

                return _contactList;
            }

        }


So where I am wrong????

This post has been edited by chris99: 19 March 2012 - 12:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: search problem with xml and linq

#2 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 208
  • View blog
  • Posts: 442
  • Joined: 07-July 10

Re: search problem with xml and linq

Posted 19 March 2012 - 04:40 AM

I think the problem might be in _dgAddressBook_DoubleClick.
int index = _dgAddressBook.SelectedItems[0].Index;

What happens if you pug _dgAddressBook.SelectedIndex;?
Was This Post Helpful? 0
  • +
  • -

#3 chris99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 09-February 10

Re: search problem with xml and linq

Posted 19 March 2012 - 06:59 AM

View PostRobin19, on 19 March 2012 - 04:40 AM, said:

I think the problem might be in _dgAddressBook_DoubleClick.
int index = _dgAddressBook.SelectedItems[0].Index;

What happens if you pug _dgAddressBook.SelectedIndex;?


No such thing...

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 208
  • View blog
  • Posts: 442
  • Joined: 07-July 10

Re: search problem with xml and linq

Posted 19 March 2012 - 08:41 AM

What type of control is _dgAddressBook?

SelectedIndices looks promising. Change it, set a debug point there, and see what those values are.

This post has been edited by Robin19: 19 March 2012 - 08:42 AM

Was This Post Helpful? 1
  • +
  • -

#5 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: search problem with xml and linq

Posted 19 March 2012 - 09:14 AM

Possibly something like: _dgAddressBook.SelectedIndices[0] would return the first element from the selected indices - in this case you only want one.
Was This Post Helpful? 0
  • +
  • -

#6 chris99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 09-February 10

Re: search problem with xml and linq

Posted 19 March 2012 - 11:02 AM

Nope...it doesn't work and it's driving me crazy...
I attached the whole solution. Try running and search for something - duble click on the search to see details and if the result is not the first from the list (with all contacts) you're in for a surprise...

Attached File  AddressBook.zip (471.68K)
Number of downloads: 22
Was This Post Helpful? 0
  • +
  • -

#7 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 208
  • View blog
  • Posts: 442
  • Joined: 07-July 10

Re: search problem with xml and linq

Posted 19 March 2012 - 12:04 PM

I see the problem now. When you do a search, you change _dgAddressBook's item list. It no longer matches indexes of ContactList. I put a break point at if (_dgAddressBook.SelectedItems.Count <= 0). I could see that SelectedItems was pulling the first name. You can use generics to get the contact out of the list.

string selectedFirstName = _dgAddressBook.SelectedItems[0].Text;
var selectedContact = XMLParse.ContactList.Where(x => x.FName == selectedFirstName).First();


If you need to get the index, you can just query against the ContactList again.
int selectedIndex = XMLParse.ContactList.IndexOf(selectedContact);

This post has been edited by Robin19: 19 March 2012 - 12:06 PM

Was This Post Helpful? 1
  • +
  • -

#8 chris99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 09-February 10

Re: search problem with xml and linq

Posted 19 March 2012 - 02:41 PM

Thank you very much for your clarification.

Now everything works O.K.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1