Loading values from a select in my GridView

2

My DAL that does Select and saves it in type list

 using System;
 using System.Data;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using MySql.Data;
 using MySql.Data.MySqlClient;
 using Geax1.Model;

 namespace Geax1.DAL
{
public class ListaVeiculosDAL
{
    private static List<_Veiculos> lv = new List<_Veiculos>();
    public static void ListaVeiulos(_Clientes obj)
    {
        using (var conn = new MySqlConnection("server=127.0.0.1;Database=xpto;User ID=root;Password='';"))
        {
            try
            {
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = new MySqlCommand("SELECT * FROM tab_veiculo ORDER BY id;", conn);

                DataSet dataset = new DataSet();
                adapter.Fill(dataset);

                foreach (DataRow linha in dataset.Tables[0].Rows)
                {
                    _Veiculos veiculo = new _Veiculos();

                    veiculo.Modelo1 = Convert.ToString(linha["modelo"]);
                    veiculo.Placa1 = Convert.ToString(linha["placa"]);
                    veiculo.Quilometragem1 = Convert.ToString(linha["quilometragem"]);
                    veiculo.Cor1 = Convert.ToString(linha["cor"]);
                    veiculo.Chassi1 = Convert.ToString(linha["chassi"]);
                    veiculo.Quilometragem1 = Convert.ToString(linha["tipo"]);

                    lv.Add(veiculo);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }

    public static List<_Veiculos> retornaVeiculo()
    {
        return lv;
    }
  }
}

Calling my method inside my GridView . However, running the page is blank, it returns no select value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Geax1.DAL;
using Geax1.Model;

namespace Geax1.Views
{
 public partial class ListagemVeiculos : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {
         GrdVeiculo.AutoGenerateColumns = true;
         GrdVeiculo.DataSource = ListaVeiculosDAL.retornaVeiculo();
         GrdVeiculo.DataBind();
    }
  }
}
    
asked by anonymous 24.09.2014 / 17:41

4 answers

1

You probably failed to activate AutoGenerateColumns .

protected void Page_Load(object sender, EventArgs e)
{
    GrdVeiculo.AutoGenerateColumns = true;
    GrdVeiculo.DataSource = ListaVeiculosDAL.retornaVeiculo();
}
    
25.09.2014 / 00:49
2

The ExecuteNonQuery method is usually used when you want to manipulate the data in the database, for example to execute an UPDATE when you just need to know the number of rows affected.

Instead of using MySqlCommand , use MySqlDataAdapter to read the rows returned by a SELECT.

Example:

MySqlConnection conn = new MySqlConnection(connection);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand("SELECT * FROM...", conn);

DataSet dataset = new DataSet();
adapter.Fill(dataset);

You can now cycle through the lines in dataset.Tables[0].Rows() and create objects to populate a list ( List<SeuObjeto> ).

Example:

List<Veiculo> veiculos = new List<Veiculo>();

foreach (DataRow linha in dataSet.Tables[0].Rows)
{
    Veiculo veiculo = new Veiculo();

    veiculo.Placa = linha["PLACA"];
    veiculo.Marca = linha["MARCA"];
    veiculo.Ano = linha["ANO"];

    veiculos.Add(veiculo);
}

See: MySqlDataAdapter .

And more complete documentation on DataAdapter of ADO.NET: Populating a DataSet from a DataAdapter .

    
24.09.2014 / 18:31
0

See the example of my code

    public Entities.Usuarios[] ListaUsuarios()
    {
        List<Entities.Usuarios> lstUsuarios = new List<Entities.Usuarios>();

        Data.Connection connection = new Data.Connection(this.ConnectionString);
        connection.AbrirConexao();

        StringBuilder sqlString = new StringBuilder();
        sqlString.AppendLine("select * from usuarios");

        IDataReader reader = connection.RetornaDados(sqlString.ToString());

        int idxId = reader.GetOrdinal("ID_USUARIO");
        int idxNome = reader.GetOrdinal("NOME_USUARIO");
        int idxEmail = reader.GetOrdinal("EMAIL_USUARIO");
        int idxLogin = reader.GetOrdinal("LOGIN_USUARIO");
        int idxSenha = reader.GetOrdinal("SENHA_USUARIO");
        int idxAtivo = reader.GetOrdinal("ATIVO_USUARIO");

        while (reader.Read())
        {
            Entities.Usuarios _Usuario = new Entities.Usuarios();
            _Usuario.Id = reader.GetInt32(idxId);
            _Usuario.Nome = reader.GetString(idxNome);
            _Usuario.Email = reader.GetString(idxEmail);
            _Usuario.Login = reader.GetString(idxLogin);
            _Usuario.Senha = reader.GetString(idxSenha);
            _Usuario.Ativo = reader.GetInt32(idxAtivo) == 1;

            lstUsuarios.Add(_Usuario);
        }

        connection.FechaConexao();

        return lstUsuarios.ToArray();
    }
    
26.09.2014 / 18:34
-2

Paul, I've seen the problem with your code. Include:

GrdVeiculo.DataBind()

Shortly after the excerpt call

GrdVeiculo.DataSource = ListaVeiculosDAL.retornaVeiculo();

The result will be

GrdVeiculo.DataSource = ListaVeiculosDAL.retornaVeiculo();
GrdVeiculo.DataBind();

I hope it helps.

    
25.09.2014 / 00:29