1

Here's my ICommand:

public ICommand ConfirmLotSavedCommand {
        get
        {
            return new RelayCommand(ConfirmLotSaved);
        }
    }

The problem is I have deserialized data that I want to store into database after a user clicks confirm button. If the user does not click on confirm or the lot number already exists, then I don't want to save the deserialized string in db.

I had trouble calling a function with one parameter inside my ConfirmLotSaved() method because of scope.

So I created a set the deserialized lot as a field and put the code to save to db inside of ConfirmLotSaved(). However, the field is null for some strange reason... I'm not sure why.

Here's my attempt:

private LotInformation lot; //field that is supposed to contain all the deserialized info 

private void ConfirmLotSaved()
    {

        using (var db = new DDataContext())
        {
            bool lotNumDbExists = db.LotInformation.Any(r => r.lot_number == DeserialLotNumber);
            if (lotNumDbExists == false)
            {
                successWindow.Message = "Successfully Saved Lot";
                dialogService.ShowDialog(successWindow.Message, successWindow);

                LotInformation newLot = new LotInformation();

                if (newLot != null)
                {

                    newLot.Id = lot.Id;
                    newLot.lot_number = lot.lot_number;
                    newLot.exp_date = lot.exp_date;

                    LotNumber = Lot.lot_number;
                    ExpirationDate = Lot.exp_date.ToString();

                    foreach (Components comp in lot.Components)
                    {
                        newLot.Components.Add(comp);

                    }
                    ComponentsList = newLot.Components;

                    foreach (Families fam in lot.Families)
                    {

                        newLot.Families.Add(fam);
                    }
                    FamiliesList = newLot.Families;

                    try
                    {
                        db.LotInformation.Add(newLot);
                        db.SaveChanges();

                        //Grabs the lot_number column from db that is distinct
                        var lotNum = db.LotInformation.GroupBy(i => i.lot_number).Select(group => group.FirstOrDefault());

                        //Loops through the lot numbers column in db and converts to list 
                        foreach (var item in lotNum)
                        {
                            Console.WriteLine(item.lot_number);
                        }
                        LotNumList = lotNum.ToList();

                        Console.WriteLine("successfully");
                    }
                    catch
                    {
                        //TODO: Add a Dialog Here

                    }
                }
                else if (lotNumDbExists == true)
                {
                    // Inform user that the lot_number already exists
                    errorWindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                    dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                    logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                    return;


                }
            }

        }
    }

Deserialization function to see where lot is grabbing data:

 public void DeserializedStream(string filePath)
    {

        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.ElementName = "lot_information";
        xRoot.IsNullable = false;

        // Create an instance of lotinformation class.
       LotInformation lot = new LotInformation();

        // Create an instance of stream writer.
        TextReader txtReader = new StreamReader(filePath);

        // Create and instance of XmlSerializer class.
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation), xRoot);

        // DeSerialize from the StreamReader
        lot = (LotInformation)xmlSerializer.Deserialize(txtReader);

        // Close the stream reader
        txtReader.Close();

        LotInformation newList = new LotInformation();

        using (var db = new DDataContext())
        {

            bool isDuplicate = db.LotInformation.Any(r => r.lot_number == lot.lot_number);

            if (newList != null && isDuplicate == false)
            {

                newList.Id = lot.Id;
                newList.lot_number = lot.lot_number;
                newList.exp_date = lot.exp_date;

                DeserialLotNumber = newList.lot_number;
                DeserialExpirationDate = newList.exp_date.ToString();

                foreach (Component comp in lot.Components)
                {
                    newList.Components.Add(comp);

                }
                DeserialComponentsList = newList.Components;

                foreach (Families fam in lot.Families)
                {

                    newList.Families.Add(fam);
                }
                DeserialFamiliesList = newList.Families;
            }
            else if (isDuplicate == true)
            {
                DeserialAnalytesList = null;
                DeserialFamiliesList = null;
                // Inform user that the lot_number already exists
                errorWindow.Message = LanguageResources.Resource.Lot_Exists_Already;
                dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
                logger.writeErrLog(LanguageResources.Resource.Lot_Exists_Already);
                return;
            }
        }

    }
Kala J
  • 2,040
  • 4
  • 45
  • 85
  • @JohnSaunders, opps sorry. Thanks for the edit. – Kala J May 14 '14 at 19:36
  • I don't see enough information here to see why the external field is null. Can you set a breakpoint where you believe its supposed to be set and determine whats going wrong? – BradleyDotNET May 14 '14 at 19:37
  • @BradleyDotNET, I added some more information... It seems that the lot itself is null when I try to set newLot = lot.lot_number etc – Kala J May 14 '14 at 19:50
  • Figured it out!! It was because I re-declared lot locally! – Kala J May 14 '14 at 20:17

2 Answers2

1

I figured out what was wrong:

After setting private LotInformation lot; field before constructor, I redeclared locally my mistake:

 LotInformation lot = new LotInformation();

Changed it to:

lot = new LotInformation(); 

and it works.

Kala J
  • 2,040
  • 4
  • 45
  • 85
0

I suggest you to use RelayCommand's generic edition http://www.kellydun.com/wpf-relaycommand-with-parameter/

It will allow you to pass lot to your command from view, all you need to store lot in current DataContext.

Mike
  • 3,766
  • 3
  • 18
  • 32