简体   繁体   中英

C# page keeps sending old data

So I hava a page where the user gives data for the app to send it to a database. When the user clicks on next the app navigates to the next page. But when the user goes back and edits the data the app still saves the old data from the first input to the database.

For example:

Name : Jon Do

The user made a mistake and goes to the previous pageL

Name : John Doe

The user clicks next and the data gets saved to the database. But except of saving the new data "John Doe" it sends the old data, "Jon Do". This, ofcourse, should not happen. I have no clue why this happens.

Here is my C# code of the page where the user should give his/her data

private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        if (ckbGegevens.IsChecked == false)
        {
            try
            { 
                dt.saveData = true;
                dt.bedrijfsNaam = txxBvName.Text;
                dt.contactPersoon = txxContPersn.Text;
                dt.telNummer = Convert.ToInt32(txxTelNr.Text);
                dt.eMail = txxEMail.Text;
                dt.land = txxLand.Text;
                dt.plaats = txxPlaats.Text;
                dt.postcode = txxPostCode.Text;

                postToJson.post("bvg");
                this.NavigationService.Navigate(new Doelen());
            }
            catch (Exception) 
            {
                MessageBox.Show("Er ontbreken gegevens!\nOf u heeft ongeldige gegevens ingevuld!");
            }

        }
        else
        {
            try {
                dt.bedrijfsNaam = txxBvName.Text;
                dt.contactPersoon = txxContPersn.Text;
                dt.telNummer = Convert.ToInt32(txxTelNr.Text);
                dt.eMail = txxEMail.Text;
                dt.land = txxLand.Text;
                dt.plaats = txxPlaats.Text;
                dt.postcode = txxPostCode.Text;
                dt.saveData = false;
                MessageBox.Show("Uw gegevens worden niet opgeslagen.\nVink voor optimaal gebruik deze functie aan.");
                this.NavigationService.Navigate(new Doelen());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

This is how I save it to the database:

 static string bedrijfsNaam = dt.bedrijfsNaam;
    static string ContPers = dt.contactPersoon;
    static int TelNum = dt.telNummer;
    static string email = dt.eMail;
    static string Land = dt.land;
    static string Plaats = dt.plaats;
    static string PostCode = dt.postcode;
    static string json;
    static string b64encode;

    public postToJson(string reqCat)
    {

    }

    public static void post(string reqCat)
    {
        if (reqCat == "bvg")
        {
            json = "{\"bedrijfsNaam\":\"" + bedrijfsNaam + "\"," +
                "\"ContPers\":\"" + ContPers + "\"," +
                "\"TelNum\":\"" + TelNum + "\"," +
                "\"email\":\"" + email + "\"," +
                "\"Land\":\"" + Land + "\"," +
                "\"Plaats\":\"" + Plaats + "\"," +
                "\"PostCode\":\"" + PostCode + "\"}";

            var b64bytes = System.Text.Encoding.UTF8.GetBytes(json);
            b64encode = System.Convert.ToBase64String(b64bytes);
            var data = new NameValueCollection();
            data["b64string"] = b64encode;
            data["filename"] = dt.bedrijfsNaam;

            using (WebClient client = new WebClient())
            {
                var sendB64 = client.UploadValues("http://" + ConfigurationManager.AppSettings["scripturi"].ToString() + "SalesKicker.php", "POST", data);
            }
        }
    }

The problem isn't in the PHP script so I don't have to post that script. I know this because I printed out the result of the JSON. Which always has the data from the first input. Can someone please tell me what is going on here?

The assignments seem to be done in the declaration of the static class, thereby they will only happen once and you don't know when. Therefore you should put these in a separate method:

static string bedrijfsNaam = dt.bedrijfsNaam;
static string ContPers = dt.contactPersoon;
static int TelNum = dt.telNummer;
static string email = dt.eMail;
static string Land = dt.land;
static string Plaats = dt.plaats;
static string PostCode = dt.postcode;

And then call that method in the post method.

Eg

private static void updateData() {
    bedrijfsNaam = dt.bedrijfsNaam;
    ContPers = dt.contactPersoon;
    TelNum = dt.telNummer;
    email = dt.eMail;
    Land = dt.land;
    Plaats = dt.plaats;
    PostCode = dt.postcode;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM