Load Datagridview with Mysql

Asked

Viewed 886 times

1

I have the following problem to display the data in a pulling gridView of Mysql database

public string CarregarAluno()
    {
        string retorno = "";

            string sql = "select * from alunos";

            MySqlConnection conn = CriarConexao();
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

            DataTable data = new DataTable();
            adapter.Fill(data);

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            retorno = "";

        }
        catch (MySqlException ex)
        { retorno = "Erro ao Carregar Grid: " + ex.Message; }

        finally { if (conn.State == ConnectionState.Open) conn.Close(); }

        return data;
    }

"Return data;" is underlined with the following message: "It is not possible to convert implicitly type System.Data.Data.Table into String "

  • date is a DataTable how do you want to return in a method that expects a string ? https://www.treinaweb.com.br/curso/csharp-basico

3 answers

1

João Víctor,

the problem is in the type of method

public datatable Carregaraluno()

Change to

public DataTable CarregarAluno()
{
    string retorno = "";

    string sql = "select * from alunos";

    MySqlConnection conn = CriarConexao();
    MySqlCommand cmd = new MySqlCommand(sql, conn);
    MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

    DataTable data = new DataTable();
    adapter.Fill(data);

    try
    {
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        retorno = "";
        return data;
    }
    catch (MySqlException ex)
    { 
        retorno = "Erro ao Carregar Grid: " + ex.Message; 
    }

    finally 
    {
        if (conn.State == ConnectionState.Open) conn.Close(); 
    }
}
  • Now this error looks like this: https://i.imgur.com/Ecarsbj.png

1


The signature of the method CarregarAluno is returning a string and at a certain point you try to return a DataTable. This is your main compilation problem.

To resolve, change your method signature to public DataTable CarregarAluno().

And the body of the method for

string sql = "select * from alunos";

MySqlConnection conn = CriarConexao();
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

DataTable data = new DataTable();
adapter.Fill(data);

try
{
   conn.Open();
   cmd.ExecuteNonQuery();
   conn.Close();
   return data;
}
catch (MySqlException ex)
{ 
   throw new Exception($"Erro ao Carregar Grid: {ex.Message}"); 
}
finally 
{
   if (conn.State == ConnectionState.Open) conn.Close(); 
}
return null;
  • If you continue any build error, add here in the commit that I try to fix the code.

  • It worked, thank you very much

1

You don’t need the class Mysqldataadapter (which is unnecessary, even has a low performance by its structure), only then would it solve, example:

public DataTable CarregarAluno()
{
    string sql = "select * from alunos";
    MySqlConnection conn = CriarConexao();
    conn.Open();
    MySqlCommand cmd = new MySqlCommand(query, connection);
    DataTable data = new DataTable();
    data.load(cmd.ExecuteReader());
    conn.Close();
    conn.Dispose();
    return data;
}

your return was also wrong, the method should return the Datatable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.