Search code examples
c#asp.net-web-apiwebformsjson.netaspxgridview

Getting error, while trying consume API and display the contents in gridview of ASP.NET Webform


Error: Target type System.Collections.IEnumerable is not a value type or a non-abstract class. Parameter name: targetType.

I was trying to consume Web API endpoint with below gridview and C# function. But I'm getting this error. I will post my Json structure also here.

I'm learning phase of .NET and any help would be much appreciated.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="False" AllowSorting="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CssClass="table table-striped table-bordered">
    <HeaderStyle CssClass="header-style" />
    <Columns>
        <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="PlantLineProcess" HeaderText="PlantLineProcess" SortExpression="PlantLineProcess" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="DefectLocation" HeaderText="Defect Location" SortExpression="DefectLocation" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Defect" HeaderText="Defect" SortExpression="Defect" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="NMR_Number" HeaderText="NMR Number" SortExpression="NMR_Number" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ControlStyle-CssClass="text-center" />
        <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" ControlStyle-CssClass="text-center" />
    </Columns>
</asp:GridView>

This code is from page_load method:

if (!Page.IsPostBack)
{
    try
    {
        string apiUrl = "https://itapps/nmrapi/api/nmr";

        HttpClient httpClient = new HttpClient();
        HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
        string jsonData = await response.Content.ReadAsStringAsync();

        // Check if the JSON data is an array or a single object
        dynamic responseData = JsonConvert.DeserializeObject(jsonData);
        dynamic data;

        if (responseData is JArray)
        {
            data = responseData;
        }
        else
        {
            data = responseData.data;
        }

        // Bind the data to the GridView
        DataTable table = new DataTable();
        table.Columns.Add("id", typeof(int));
        table.Columns.Add("Type", typeof(string));
        table.Columns.Add("PlantLineProcess", typeof(string));
        table.Columns.Add("DefectLocation", typeof(string));
        table.Columns.Add("Defect", typeof(string));
        table.Columns.Add("Description", typeof(string));
        table.Columns.Add("NMR_Number", typeof(string));
        table.Columns.Add("Quantity", typeof(int));
        table.Columns.Add("Status", typeof(string));
        table.Columns.Add("Date", typeof(string));

        foreach (var item in data)
        {
            table.Rows.Add(item.id, item.Type,
                           item.PlantLineProcess,
                           item.DefectLocation,
                           item.Defect,
                           item.Description,
                           item.NMR_Number,
                           item.Quantity,
                           item.Status,
                           item.Date);
        }

        GridView1.DataSource = data;
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Error: " + ex.Message;
    }
}

This is my part of JSON (as of now, I have entered data for 4 ids alone. This JSON is long and having 72 ids)

{
"success":true,
"message":null,
"data":"[{\"id\":1,\"Type\":\"Neeraj\",\"SerialNo\":\"DFR45566678\",\"PlantLineProcess\":\"new jackpot\",\"Date\":\"0001-01-01T00:00:00\",\"DefectLocation\":\"First Corner\",\"Defect\":\"AOL\",\"Description\":\"my description\",\"NMR_Number\":\"7383\",\"Quantity\":10,\"Status\":0},{\"id\":2,\"Type\":\"Jack\",\"SerialNo\":\"BL11099035474\",\"PlantLineProcess\":\"new jackpot\",\"Date\":\"0001-01-01T00:00:00\",\"DefectLocation\":\"First Corner\",\"Defect\":\"AOL\",\"Description\":\"my description\",\"NMR_Number\":\"7383\",\"Quantity\":1,\"Status\":0},{\"id\":3,\"Type\":\"Jack\",\"SerialNo\":\"BL11099035474\",\"PlantLineProcess\":\"new jackpot\",\"Date\":\"0001-01-01T00:00:00\",\"DefectLocation\":\"First Corner\",\"Defect\":\"AOL\",\"Description\":\"my description\",\"NMR_Number\":\"7383\",\"Quantity\":1,\"Status\":0}]"
}

Solution

  • From your JSON, data is a string.

    When you deserialize jsonData, response.data is JValue type but not an IEnumerable.

    You have to deserialize once more for response.data with either of these:

    data = JArray.Parse(responseData.data.ToString());
    

    Or

    data = JsonConvert.DeserializeObject(responseData.data.ToString());