2
My DAL who does the Select and saved in the list type<>
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 into mine Gridview. However, when executing the page is empty, it does not return any value of the select.
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();
}
}
}
By the way, this exception treatment of yours doesn’t make much sense :-) You are capturing an exception to simply relaunch it. For this purpose you may simply not have the exception treatment (eliminate the
try..catch
).– Caffé
Make your DAL return the Dataset itself that was filled in by the Dataadapter (
adapter.Fill(dataset)
) and connect the grid datasource directly to Dataset:GrdVeiculo.DataSource = dataset
. Either this or you will need to implement your vehicle list so that it is a valid data source.– Caffé
This way didn’t work. Grdveiculo.Datasource = dataset
– Vinícius
Now it is debug. For example, dataset has data?
– Caffé
Is there any way you can join the chat? I’ll explain it better there.
– Vinícius