How to show database data in Textboxes?

Asked

Viewed 2,527 times

0

Hello! My scenario is as follows:

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

Below one of the controls of my file .aspx where I wish to bring a certain bank information

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

And down with mine .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 method ClienteBusiness.ListarCliente(idCliente) calls another method that, in turn, makes the query to the bank, 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;
    }
  • Are you experiencing what difficulty specifically? The method is returning some exception at some point?

  • Hello Peter! The data just isn’t displayed. I don’t get any Exception or error.

  • "Debugging" the code you checked if you are entering the if and values are being displayed? If yes, you can go "debugging" by placing it in the Visual Studio Watch window to see if at any point in the request flow this value is deleted.

  • Pedro, there were some errors in my methods, which Eduardo listed and so I was able to solve the problem. Still, thank you for your help!

1 answer

2


There are some problems in your code:

  1. In the Page_Load the command ClienteBusiness.ListarCliente(idCliente); does not populate the object cliente, since nothing receives the return of the métood listarCliente(...).
  2. The method public static List<TB_CLIENTE_CLI> ListarCliente(int idCliente) returns a list while it should return only one client object, so that its code in the Page_Loadmake sense.

The line ClienteBusiness.ListarCliente(idCliente); in the method Page_Load should be: cliente = ClienteBusiness.ListarCliente(idCliente);.

The method public static List<TB_CLIENTE_CLI> ListarCliente(int idCliente) 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();
}
  • Thanks for the corrections, Eduardo! I managed to solve the problem according to your recommendations.

Browser other questions tagged

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