0

I have the code below:

if (jumlahiddb < jumlahbuku)
{
        DownloadBukuKomik(url);

        string KomikUpdate = @"INSERT INTO books (id,title,folder_id,identifier) SELECT " + intID + ",'" + namaFile + ".pdf',67,'" + namaFile +
             ".pdf' WHERE not exists (select id AND title AND folder_id AND identifier FROM books WHERE id=" + intID + " and title='" + namaFile +
             ".pdf' AND folder_id=67 and identifier='" + namaFile + ".pdf')";
        Debug.WriteLine(KomikUpdate.ToString());
        var komikQuery = objConnUpdate.Prepare(KomikUpdate);
        komikQuery.Step();
}
else
{
    bool shown = false;
    if (!shown)
    {
        MessageDialog messageDialog1 = new MessageDialog("Jumlah komik bertambah sebanyak " + jumlahbuku + " komik pada menu Komik Pendidikan", "Update Berhasil");
        messageDialog1.Commands.Add(new UICommand("OK", (command) =>
        {
            DownloadBukuVideo.IsOpen = false;
            Downloading.IsOpen = false;
            ukomikBtn.Visibility = Visibility.Visible;
            downloadKomikBtn.Visibility = Visibility.Collapsed;
            ukomikText.Visibility = Visibility.Collapsed;
            ukomikText.Text = "";
            shown = true;
        }));
         await messageDialog1.ShowAsync();
}

I have a problem, that is when I click the OK button, it will display message dialog again. I want message dialog shown only 1 time. How to solve it?

Rose
  • 613
  • 4
  • 22
  • 2
    Your `shown` variable is locally scoped to the `else` part of your conditional. Therefore it will be created and set to false every time that piece of code is called. You need to scope it at a higher level if you're going to use that method - without seeing the rest of your code, perhaps as a class-level variable declared outside of the method with the code shown above. As an aside, the way you are concatenating your SQL query opens you up to SQL injection attacks - take a look at how to use _parameterised queries_ instead. – Diado May 03 '18 at 07:54
  • @Diado I've tried to make it in the variable class, but the message dialog still displayed repeatedly (according to the number of loops) – Rose May 04 '18 at 04:22
  • Without seeing the relevant code, i.e. where you're declaring and setting the variable now, it's hard to give you any suggestions on what could be wrong – Diado May 04 '18 at 08:34
  • @Diado here the sample: https://1drv.ms/t/s!Auqiv8Ukng7U7lo22fdNppsDQpBT – Rose May 04 '18 at 08:48

1 Answers1

0

The problem is that you are declaring the shown variable in a local scope, so it is being initialised and set every time your show message box code is run.

To avoid this, declare it at a higher level - for instance, at class level. For example, based on the code you shared in the comments:

class myClass {

    private bool _shown;

    public async void KomikMsgDialog()
    {
        if (!_shown) // If we haven't shown the dialog yet
        {
            MessageDialog messageDialog1 = new MessageDialog("Jumlah komik bertambah sebanyak " + jumlahbuku + " komik pada menu Komik Pendidikan", "Update Berhasil");
            messageDialog1.Commands.Add(new UICommand("OK", (command) =>
            {
                DownloadBukuVideo.IsOpen = false;
                Downloading.IsOpen = false;
                ukomikBtn.Visibility = Visibility.Visible;
                downloadKomikBtn.Visibility = Visibility.Collapsed;
                ukomikText.Visibility = Visibility.Collapsed;
                ukomikText.Text = "";
            }));
            await messageDialog1.ShowAsync();
            _shown = true; // Flag the dialog as having been shown
        }
    }
}

This way, the first time you call the method it will check whether the the dialog has been shown, which it won't have been, so it will show the dialog and flag it as having been shown. The next time it will check the flag and not show the dialog.

Diado
  • 2,229
  • 3
  • 18
  • 21