Reputation: 121
At the moment when i use this, i have to re-launch the app to get the data to refresh. There are several posts saying i should put a random number on the end, but i can't do that as i put a number on the end for it to work.
What is the solution?
void myButton_Click(object sender, RoutedEventArgs e)
{
i = 0;
lstService.Items.Clear();
lstDestination.Items.Clear();
listTime.Items.Clear();
if (textSearchBox.Text == "Cranleigh Avenue")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "6445";
}
if (textSearchBox.Text == "Cranleigh Avenue (briamaw)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7099";
}
else if (textSearchBox.Text == "Brighton Station (stop A)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7052";
}
else if (textSearchBox.Text == "Brighton Station (stop B)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7053";
}
else if (textSearchBox.Text == "Brighton Station (stop C)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7054";
}
else if (textSearchBox.Text == "Brighton Station (stop D)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7055";
}
else if (textSearchBox.Text == "Sea Life Centre (stop K)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7642";
}
else if (textSearchBox.Text == "Race Hill")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "6724";
}
string myvar = txtstopId.Text;
textSearchBox.Text = "";
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://www.URL.aspx?stopid=" + HttpUtility.UrlEncode(myvar));
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public int i;
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(RootContainer));
RootContainer rootContainer = ser.ReadObject(e.Result) as RootContainer;
foreach (Departures em in rootContainer.Departures)
{
i = i + 1;
if (i < 9)
{
string id = em.ServiceName;
string dt = em.Destination;
string tm = em.DepartureTimeAsString;
lstService.Items.Add(id);
lstDestination.Items.Add(dt);
listTime.Items.Add(tm);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Edit
WebClient webClient = new WebClient();
string url = string.Format("http://www.URL.aspx?stopid={0}&ts={1}", HttpUtility.UrlEncode(myvar) + DateTime.Now.Ticks);
Uri uri = new Uri(url);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
Upvotes: 0
Views: 594
Reputation: 2216
I would use a timestamp not a random number:
string url = string.Format("http://www.URL.aspx?stopid={0}&ts={1}",
HttpUtility.UrlEncode(myvar),
DateTime.Now.Ticks);
Uri uri = new Uri(url);
Upvotes: 3