Gridview c # does not show data

0

I have the following problem: I use a rest webservice to fill a list of employees. Then he used that list to fill a Gridview but at the time of running the DataBind he does not paint anything on the form:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace WebServiceNetClienteRest{
    public partial class WebForm1 : System.Web.UI.Page {
        List<Empleado> empleados = null;
        protected void Page_Load(object sender, EventArgs e){
            grdEmpleados.AutoGenerateColumns = true;
        }
        protected void btnListar_Click(object sender, EventArgs e){
            Task tsk = consumir();
        }
        protected async Task consumir(){
            try{
                HttpClient clientWS = new HttpClient();
                clientWS.BaseAddress = new Uri("http://localhost:8080/JavaWebServiceServerCRUDRestful/restful/empleados/json/listarempleados/");
                clientWS.DefaultRequestHeaders.Accept.Clear();
                clientWS.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await clientWS.GetAsync("");
                if (response.IsSuccessStatusCode) {
                    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                    string cadena = encoding.GetString(response.Content.ReadAsByteArrayAsync().Result);
                    empleados = new JavaScriptSerializer().Deserialize<List<Empleado>>(cadena);
                    System.Diagnostics.Debug.WriteLine("\n\n\n");
                    foreach (var empleado in empleados){
                            System.Diagnostics.Debug.WriteLine("Id={0}\nNombre={1}\nEmpleado={2}", empleado.Id, empleado.Nombre, empleado.Puesto);   
                    }
                }
                System.Diagnostics.Debug.WriteLine("\n\n\n");
                grdEmpleados.DataSource = empleados;
                grdEmpleados.Visible = true;
                grdEmpleados.DataBind();
            }catch (Exception e){
                System.Diagnostics.Debug.WriteLine("\n\n\n***************:"+e.Message+ "\n\n\n*****************");
            }
        }
    }
}

And this is the aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebServiceNetClienteRest.WebForm1"  %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:Label ID="lblId" runat="server" Text="ID:"></asp:Label>
        <asp:TextBox ID="txtId" runat="server" Width="142px"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="lblNombre" runat="server" Text="Nombre:"></asp:Label>
        <asp:TextBox ID="txtNombre" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="lblPuesto" runat="server" Text="Puesto:"></asp:Label>
        <asp:TextBox ID="txtPuesto" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="btnListar" runat="server" OnClick="btnListar_Click" Text="Listar" />
        <br />
        <br />
        <asp:GridView ID="grdEmpleados" runat="server" >
        </asp:GridView>
    </div>
    </form>
</body>
</html>

When debugging the data if they are on the employee list and assigned correctly to the employees, but I do not know why they do not appear on the form, even paint this in the visual studio console:

Id=1
Nombre=Andres
Empleado=Desarrollador
Id=2
Nombre=Abraham
Empleado=Programador

Can someone support me? I am very new in this language.

    
asked by abrahamhs 28.06.2016 в 01:50
source

1 answer

1

The issue is that you are working with an asynchronous method, do not use the HttpClient using the WebClient

protected void consumir()
{
    try
    {
        var uri = new Uri("http://localhost:8080/JavaWebServiceServerCRUDRestful/restful/empleados/json/listarempleados/");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";
        request.Accept = "application/json";
        request.ContentType = "application/json";

        string cadena = "";
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            cadena = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
         }

        var empleados = new JavaScriptSerializer().Deserialize<List<Empleado>>(cadena);
        System.Diagnostics.Debug.WriteLine("\n\n\n");
        foreach (var empleado in empleados){
                System.Diagnostics.Debug.WriteLine("Id={0}\nNombre={1}\nEmpleado={2}", empleado.Id, empleado.Nombre, empleado.Puesto);   
        }

        System.Diagnostics.Debug.WriteLine("\n\n\n");

        grdEmpleados.DataSource = empleados;
        grdEmpleados.Visible = true;
        grdEmpleados.DataBind();

    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine("\n\n\n***************:"+e.Message+ "\n\n\n*****************");
    }
}

This way you will not have problems with the asynchrony that loads the gridview data

    
answered by 28.06.2016 / 03:51
source