At the moment I have a Silverlight application in Windows Phone which uses the local database. Basically a listBox is generated to show the current list of stored ‘clients’, which works fine. Now I would like to be able to let the user Edit one of the client’s details. For this, I created a new page which is loaded whenever a user clicks on a button from the main page. The event is the following:
public ClientItem selectedClient;
public void Edit_Click(object sender, EventArgs e)
{
if (clientItemsListBox.SelectedItem != null)
{
selectedClient = clientItemsListBox.SelectedItem as ClientItem;
NavigationService.Navigate(new Uri("/EditClient.xaml", UriKind.Relative));
}
}
The above simply checks which client is selected, stores it as selectedClient and navigates to the EditClient page.
In the EditClient class I have the following method:
public void saveButton_Click(object sender, RoutedEventArgs e)
{
//Get the client that is selected
ClientItem clientForDelete = mainPage.selectedClient;
mainPage.ClientItems.Remove(clientForDelete);
mainPage.clientDB.ClientItems.DeleteOnSubmit(clientForDelete);
// Create a new client based on the text boxes
ClientItem newClient = new ClientItem { ClientName = newClientNameTextBox.Text, ClientSurname = newClientSurnameTextBox.Text, ClientCompany = newClientCompanyTextBox.Text, ClientPhone = int.Parse(newClientPhoneTextBox.Text) };
// Add new client to the database
mainPage.clientDB.ClientItems.InsertOnSubmit(newClient);
// Sava database changes
mainPage.clientDB.SubmitChanges();
// Go to main screen
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
When I run this code I get a nullReferenceException when it tries to execute:
mainPage.ClientItems.Remove(clientForDelete);
This is because the selectedClient is null. How can I get the object from the other class without it being null? Because I do not want to delete the item from the main class just in case the user decides to cancel the operation. Also I want to show the details of that client when the page is loaded, which I know how to do if I manage to get the object :). thanks
You can pass the client to the other page as query parameter:
Then in your MainPage
OnNavigatedTo()retrieve the client: