How to display bank data in Textboxes?

0

Hello! My scenario is as follows:

I'm developing a web application in C # ASP.NET, and I'm having some difficulties getting the database's data (via LINQ to SQL) to the page controls.

Below is one of the controls in my .aspx where I want to bring a certain information from the bank

 <asp:TextBox 
    ID="txtCodigo" 
    ClientIDMode="Static" 
    CssClass="form-control"
    MaxLength="4"
    Enabled="false"
    runat="server">
 </asp:TextBox>

And below my .aspx.cs .

protected void Page_Load(object sender, EventArgs e)
    {            
            int idCliente;
            TB_CLIENTE_CLI cliente = new TB_CLIENTE_CLI();
            if (Int32.TryParse(Request.QueryString["ID"], out idCliente))
            {
                ClienteBusiness.ListarCliente(idCliente);

                txtCodigo.Text = cliente.ID_CLIENTE_CLI.ToString();
                txtRazaoSocial.Text = cliente.TXT_RAZAOSOCIAL_CLI;
                txtNomeFantasia.Text = cliente.TXT_NOMEFANTASIA_CLI;
                txtCNPJ.Text = cliente.TXT_CNPJ_CLI;
                txtCEP.Text = cliente.TXT_CEP_CLI;
                txtLogradouro.Text = cliente.TXT_LOGRADOURO_CLI;
            }

}

My application is divided into layers. The ClienteBusiness.ListarCliente(idCliente) method calls another method which, in turn, queries the database, as shown in the code below:

public static List<TB_CLIENTE_CLI> ListarCliente(int idCliente)
    {
        List<TB_CLIENTE_CLI> cliente = null;
        using (PlanoTesteDataContext context = new PlanoTesteDataContext())
        {
            cliente = (from clientes in context.TB_CLIENTE_CLIs
                       where clientes.ID_CLIENTE_CLI == idCliente
                       select clientes).ToList();
        }
        return cliente;
    }
    
asked by anonymous 15.07.2014 / 15:33

1 answer

2

There are some problems with your code:

  • No Page_Load the ClienteBusiness.ListarCliente(idCliente); command does not populate the cliente object, since nothing gets the return of the listarCliente(...) method.
  • The public static List<TB_CLIENTE_CLI> ListarCliente(int idCliente) method returns a list whereas it should return only one client object, so that its code in Page_Load makes sense.
  • The ClienteBusiness.ListarCliente(idCliente); line in the Page_Load method should be: cliente = ClienteBusiness.ListarCliente(idCliente); .

    The public static List<TB_CLIENTE_CLI> ListarCliente(int idCliente) method should be implemented as follows:

    public static List<TB_CLIENTE_CLI> ListarCliente(int idCliente)
    {
        List<TB_CLIENTE_CLI> cliente = null;
        using (PlanoTesteDataContext context = new PlanoTesteDataContext())
        {
            cliente = (from clientes in context.TB_CLIENTE_CLIs
                       where clientes.ID_CLIENTE_CLI == idCliente
                       select clientes).ToList();
        }
        return cliente.First();
    }
    
        
    15.07.2014 / 16:15