locked
How can I prevent adding duplicates in a “list-box mover” UI? RRS feed

  • Question

  • Hi,

    At http://blogs.msdn.com/b/lightswitch/archive/2010/12/16/how-to-create-a-many-to-many-relationship-andy-kung.aspx Andy Kung teaches us how to build a “list-box mover” UI to interact in a many to many relationship.

    In the 21 Dec 2010 3:43 PM comment Thom C suggested some code to prevent adding duplicates, but it did not work for me.

     

    partial void GenreCollection.SelectedItem()

            {

                // Write your code here.

                if (this. GenreCollection.SelectedItem != null)

                {

                    MovieGenre mgExists = new MovieGenre();

                    foreach (MovieGenre mgSearch in this.MovieGenreCollection)

                    {

                      if (mgSearch.Genre.Name == this.GenreCollection.SelectedItem.Name)

                      {

                          mgExists = mgSearch;

                      }

                    }

                    if (mgExists == null || mgExists.Genere == null)

                    {

                        MovieGenre mg = this.MovieGenreCollection.AddNew();

                        mg.Movie = this. MovieProperty;

                        mg.Genre = this. GenreCollection.SelectedItem;

                    }

                }

            }

     

    I’m working in a DetailScreen and when I try to save I get 2 validating issues (“Movie: Value can’t be null” and “Genre: Value can’t be null”-Data must be saved in order to fix those errors).

    If I only use Andys code it works.

     

    So, it’s there any other suggestion about how to prevent adding duplicates in a “list-box mover” UI?

     

    Thank you all!

    Tuesday, January 18, 2011 3:48 AM

Answers

  • I've taken a look at this sample and have recreated the error that you describe.

    It appears that the declaration of mgExists is the cause of the 2 validation issues although I can't really explain why.

    Instead of testing against myExists, I've set a boolean flag when the genre is found and this technique works  for me.

    Here's the code that I've used. Hope that helps!

    partial void Button_Execute()
    {
      if (GenreCollection.SelectedItem != null)
      {
        bool genreExists = false;
        foreach (MovieGenre mgSearch in this.MovieGenreCollection)
        {
          if (mgSearch.Genre.Name == this.GenreCollection.SelectedItem.Name)
          {
            genreExists = true;
          }
        }
    
        if (genreExists == true)
        {
          ScreenExtensions.ShowMessageBox(this, "Genre already exists");
        }
        else
        {
          MovieGenre mg = MovieGenreCollection.AddNew();
          mg.Movie = this.Movie;
          mg.Genre = GenreCollection.SelectedItem;
        }
      }
    }
    
    

     

    • Marked as answer by san_pedro Tuesday, January 18, 2011 6:32 PM
    Tuesday, January 18, 2011 1:06 PM

All replies

  • I've taken a look at this sample and have recreated the error that you describe.

    It appears that the declaration of mgExists is the cause of the 2 validation issues although I can't really explain why.

    Instead of testing against myExists, I've set a boolean flag when the genre is found and this technique works  for me.

    Here's the code that I've used. Hope that helps!

    partial void Button_Execute()
    {
      if (GenreCollection.SelectedItem != null)
      {
        bool genreExists = false;
        foreach (MovieGenre mgSearch in this.MovieGenreCollection)
        {
          if (mgSearch.Genre.Name == this.GenreCollection.SelectedItem.Name)
          {
            genreExists = true;
          }
        }
    
        if (genreExists == true)
        {
          ScreenExtensions.ShowMessageBox(this, "Genre already exists");
        }
        else
        {
          MovieGenre mg = MovieGenreCollection.AddNew();
          mg.Movie = this.Movie;
          mg.Genre = GenreCollection.SelectedItem;
        }
      }
    }
    
    

     

    • Marked as answer by san_pedro Tuesday, January 18, 2011 6:32 PM
    Tuesday, January 18, 2011 1:06 PM
  • Perfect!

    Thank you very much!!

    Tuesday, January 18, 2011 6:33 PM
  • Perfect!

    Thank you very much!!


    by the way.... you may also want to look at the Dictionary collection - it will not allow a duplicate key, there are some other base collections like that.
    Tuesday, January 18, 2011 10:11 PM
  • Expanding on the idea of using a Dictionary collection, how would you go about binding that to a Lightswitch list control on a screen?
    Wednesday, January 19, 2011 7:24 PM