Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good evening all,

I am building a WPF navigation application. I'm using Entity Framework 6 for my persistence layer. I've got my pages built. My database structure consists of two tables with a one to many relationship. You have a Defendant and the Defendant can have multiple witnesses. The ID for the Defendant table is just ID, there is an ID in the Witness table and a MasterID (links back to the DefendantId). Therefore, you cannot have a witness if there isn't a defendant to attach it to. I will attach the code that I have tried. I cannot get the ID that is being passed into the initialize constructor down to my button click event
C#
<pre lang="c#">private void AddWitness(object sender, RoutedEventArgs e)</pre>

which I need to add a new witness. The ID is being passed to the constructor though. Please help. Thanks in advance.

-Branden

What I have tried:

C#
public partial class DefendantView : Page
   {
       public DefendantView(int ID)
       {
           InitializeComponent();
           GetDefendantById(ID);

           int MasterId = ID;
       }

       public void GetDefendantById(int ID)
       {
           using(DataController context = new DataController())
           {
               DefendantModel defendant = context.GetDefendantById(ID);
               grdDefendantDetails.DataContext = defendant;
           }
       }

       private void AddWitness(object sender, RoutedEventArgs e)
       {
           int defId = (int)sender;

           DefendantModel st = new DefendantModel();
           defId = Convert.ToInt32(st.ID);
           this.NavigationService.Navigate(new AddWitnessView(defId));
       }
Posted
Updated 16-Dec-16 18:32pm

1 solution

Hmm, I'm not sure if these are the main problems, but some issues I noticed:

C#
public partial class DefendantView : Page
{
    public int MasterId { get; private set; }

    public DefendantView(int ID)
    {
        InitializeComponent();
        GetDefendantById(ID);

        //You aren't persisting MasterId to anything, it goes out of scope.
        //int MasterId = ID;
        MasterId = ID;
    }

    public void GetDefendantById(int ID)
    {
        using(DataController context = new DataController())
        {
            DefendantModel defendant = context.GetDefendantById(ID);
            grdDefendantDetails.DataContext = defendant;
        }
    }

    private void AddWitness(object sender, RoutedEventArgs e)
    {
        //If this is your button click event handler, sender is the Button
        //not an int.
        //int defId = (int)sender;
        
        DefendantModel st = new DefendantModel();
        int defId = Convert.ToInt32(st.ID);
        this.NavigationService.Navigate(new AddWitnessView(defId));
    }
}

You haven't posted the DefendantModel class, but I suspect AddWitness isn't doing what you think it is. You're creating a new DefendentModel object and getting whatever the default ID is for that class. Maybe you were looking for int defId = Convert.ToInt32(MasterId);? Hard to tell without the whole picture of what's going on :)
 
Share this answer
 
Comments
Branden Coker 17-Dec-16 10:10am    
Jon, Thanks for your response. I forgot to mention that the ID comes from another view when someone selects a Defendant from a list. That part works, if I select the first person in the list I get the correct ID same with any other person. Both my DefendantModel and WitnessModels are below. Sorry I forgot to post them. I have the data access Logic (Linq) in a separate folder.

The Defendant Model:
   public class DefendantModel
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string NameSuffix { get; set; }
        public string Alias { get; set; }
        public string SSN { get; set; }
        public string Race { get; set; }
        public string Sex { get; set; }
        public List<WitnessModel> WitnessModelList { get; set; }
        public WitnessModel WitnessModel { get; set; }
    }


The WitnessModel:
 public class WitnessModel
    {
        public int ID { get; set; }
        public int MasterID { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string NameSuffix { get; set; }
        public string Sex { get; set; }
        public string Race { get; set; }
    }


Let me know if I have left anything out. Thank you.
Jon McKee 17-Dec-16 13:34pm    
Ok, I think I know what's going on here. So this is one page in a larger application. When this page is created you're passing an ID for your defendant which is both the primary key for your defendant and a foreign key for your witnesses, retrieving the defendant data, and updating the DataContext for one of the page controls that displays that data. All good so far.

So now you have a page setup for a defendant and you want them to be able to add witnesses. So there's an "Add Witness" button somewhere on the page that is handled by AddWitness(). This event handler is responsible for setting up the witness view and passing the ID for this defendant so that ID can be used as the MasterID (foreign key) when creating new witnesses? Correct?

If so, all you need to do is propogate the constructor ID down to the handler. My example above actually did that:
//Add a property to your page
public int MasterID { get; private set; }

public DefendantView(int ID)
{
    InitializeComponent();
    GetDefendantById(ID);
    //Set that property in your constructor
    MasterID = ID;
}

private void AddWitness(object sender, RoutedEventArgs e)
{  
    //Pass the property to your witness view
    this.NavigationService.Navigate(new AddWitnessView(MasterID));
}
Branden Coker 18-Dec-16 11:04am    
Jon,
I think I can figure this out from here. Thank you for all your help. The button I'm trying to get the ID to is actually a save button on the AddNewWitness View. The view has the ID from the defendant It's a matter of converting the data from the fields to the model and persisting it is where my issue is. This is what I'm doing on that..

 private void btnSaveWitness_Click(object sender, RoutedEventArgs e)
        {
            using (DataController context = new DataController())
            {
                DefendantViewModel defendant = new DefendantViewModel();

                defendant.DefendantModel.FirstName = txtFName.Text;
                defendant.DefendantModel.MiddleName = txtMName.Text;
                defendant.DefendantModel.LastName = txtLName.Text;
                defendant.DefendantModel.NameSuffix = txtSuffix.Text;
                defendant.DefendantModel.Race = cboRace.SelectedValue.ToString();
                defendant.DefendantModel.Sex = cboSex.SelectedValue.ToString();

                context.CreateWitness(defendant);
            }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900