Question

Hi there I am a beginner with developing on C#. I am having some problems passing data from one page to another. Within a listbox I have data which i have obtained from a database via web service.

I have created some coding to move sets of selected data to the next page and input it into the assigned text blocks. Currently this coding only works for one data field "eventId."

Could you please have a look at my code and tell me what I have done wrong and how i can fix this.

Here is my coding from the page which holds the listbox with the sets of data:

private void FirstListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        EventServiceReference1.Event myEvent = (EventServiceReference1.Event)FirstListBox2.SelectedItem;
        int eventId = myEvent.EventID;
        string eventList = myEvent.EventList;
        string eventDescription = myEvent.Description;
        string eventDate = myEvent.Date;
        string eventTime = myEvent.Time;
        string eventLocation = myEvent.Location;
        var url = string.Format("/EventPageTemp.xaml?eventId={0}", eventId + "&eventList={0}", eventList);
        NavigationService.Navigate(new Uri(url, UriKind.Relative));

    }

Here is my coding from the "EventPageTemp" page which i am passing the data to:

        int eventId;
        string eventIdStr;
        string eventList;

        if (NavigationContext.QueryString.TryGetValue
            ("eventId", out eventIdStr) && int.TryParse(eventIdStr, out eventId))
        {// load event data, and set data context
            txtEID.Text = eventIdStr;}

        if (NavigationContext.QueryString.ContainsKey("eventList"))
        {
            string val = NavigationContext.QueryString["eventList"];
            txtEList.Text = eventList;
        }

At the moment it is coming up with the errors: - the name 'eventList' does not exist in current context -use of unassigned local variable 'eventList'

Please can you help me figure this out. Thank you.

Was it helpful?

Solution

the issue is your url, eventId + "&eventList={0}", eventList will be pass as eventId:

var url = string.Format("/EventPageTemp.xaml?eventId={0}", eventId + "&eventList={0}", eventList);

it should be:

var url = string.Format("/EventPageTemp.xaml?eventId={0}&eventList={1}", eventId, eventList);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top