0

This is WinForm app. The ProgressBarz class is a simple progress bar I've created and it appears only when ImportaOrdiniAsync method finishes. This method behaves like a synchronous one.

private async void ultraButtonImporta_Click(object sender, EventArgs e)
{
    if (ultraComboStagione.Value != null)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Title = "Seleziona file da Importare";
        openFileDialog.DefaultExt = "xlsx";
        openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            var task = ImportaOrdiniAsync(openFileDialog.FileName,
                ultraComboStagione.Value.ToString());
            ProgressBarz caricamento = new ProgressBarz ();
            caricamento.Show();
            Application.OpenForms["OrdiniLimiti"].Enabled = false;
            await task;
            caricamento.Close();
            Application.OpenForms["OrdiniLimiti"].Enabled = true;
        }
        else
        {
            MessageBox.Show("Selezionare un file Excel!");
        }
    }
    else
    {
        MessageBox.Show("Selezionare una Stagione!");
    }
}

private async Task ImportaOrdiniAsync(string fileName, string stagione)
{
    Workbook wb = Workbook.Load(fileName);
    Worksheet ws = wb.Worksheets[0];

    for (int i = 1; i < ws.Rows.Count(); i++)
    {
        //Validation Here...

        int idOrdine = await IsOrdinePresenteAsync(codiceCliente, stagione);
        if (idOrdine != -1)
        {
            //Other stuff
        }
        else
        {
            //Other stuff
        }
    }

    //Other stuff
}

private async Task<int> IsOrdinePresenteAsync(string codiceCliente, string stagione)
{
    using (MySqlConnection conn = new MySqlConnection(
        AdoFunction.brMySqlConnection.brMyStringConnection))
    {
        MySqlCommand cmd = new MySqlCommand("SELECT id from table " +
            "WHERE field1 = @codiceCliente AND field2 = @stagione", conn);
        cmd.Parameters.AddWithValue("@codiceCliente", codiceCliente);
        cmd.Parameters.AddWithValue("@stagione", stagione);

        await conn.OpenAsync();
        object result = await cmd.ExecuteScalarAsync();
        if (result != null)
        {
            return (int)result;
        }

        return -1;
    }
}

The problem is in IsOrdinePresenteAsync method in particular with OpenAsync() or ExecuteScalarAsync(). I tried using an await Task.Delay inside IsOrdinePresenteAsync and everything works fine.

2
  • 1
    Which version of MySqlConnector are you using? bugs.mysql.com/bug.php?id=70111 Fixed in version 8.0.33 Commented Jun 19, 2023 at 11:36
  • @Charlieface i'm using 6.9.8.0 Thank You. Commented Jun 19, 2023 at 11:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.