# How to bind searchResult from ElasticSearch into Gridview (C# ASP.NET) by using NEST

**URL:** <https://discuss.elastic.co/t/how-to-bind-searchresult-from-elasticsearch-into-gridview-c-asp-net-by-using-nest/180649>\
**Category:** Elasticsearch\
**Created:** [May 11, 2019, 12:41pm UTC](https://discuss.elastic.co/t/how-to-bind-searchresult-from-elasticsearch-into-gridview-c-asp-net-by-using-nest/180649 "2019-05-11T12:41:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LawrenceSPK](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@LawrenceSPK](https://discuss.elastic.co/u/LawrenceSPK)\
**Post date:** [May 11, 2019, 12:41pm UTC](https://discuss.elastic.co/t/how-to-bind-searchresult-from-elasticsearch-into-gridview-c-asp-net-by-using-nest/180649/1 "2019-05-11T12:41:17Z")

</div>

I need to bind the search result from NEST (ElasticSearch) to a `Gridview` in [ASP.NET](http://ASP.NET) (Webform).

Code I get the result from ElasticSearch from using NEST:

```auto
public class Address
{
    public int SN { get; set; }
    public string JLN { get; set; }
}

protected void BtnSearch_Clicked(object sender, EventArgs e)
{
    string SearchValue = txtSearchValue.Text;
    string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
    string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
    string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

    var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
        .DefaultIndex("masterlist*");

    var client = new ElasticClient(settings);

    var searchResponse = client.Search<Address>(s => s
            .Index("masterlist*")
            .From(0)
            .Size(10)
            .Query(q => q
                 .QueryString(qs => qs
                    .Query("JLN:\""+ SearchValue +"\"")
                )
            )
        );

    var address = searchResponse.Documents.ToList();

    ESGridview.DataSource = address;
    ESGridview.DataBind();
}

```

With this code, the gridview can auto-generate two fields of correct header which is "SN" and "JLN", and it can auto generate 10 rows (I limit the size to 10 rows max in search syntax) but it's empty data in the column.

I did found another POST with this link  
[https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/returned-fields.html#returned-fields](https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/returned-fields.html#returned-fields)  
After check with this link,  
I changed my code to

```auto
string SearchValue = txtSearchValue.Text;
string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
    .DefaultIndex("masterlist*");

var client = new ElasticClient(settings);

var searchResponse = client.Search<Address>(s => s
    .StoredFields(sf => sf
        .Fields(
            f => f.SN,
            f => f.JLN
            )
         )
        .From(0)
        .Size(10)
        .Query(q => q
             .QueryString(qs => qs
                .Query("JLN:\""+ SearchValue +"\"")
            )
        )
    );

foreach (var fieldValues in searchResponse.Fields)
{
    var document = new
    {
        SN = fieldValues.ValueOf<Address, int>(p => p.SN),
        JLN = fieldValues.Values<Address, string>(p => p.JLN)
    };
}

var address = searchResponse.Documents;

ESGridview.DataSource = address;
ESGridview.DataBind();

```

But I get an error while run the code from start on whole **foreach (var...)** area :

```
System.NullReferenceException:'Object reference not set to an instance of an object.'

```

Did anyone can teach me how can solve this problem or anything I do fault ?  
Many many thanks ~

**ElasticSearch 7.0.1**  
**NEST 7.0.0**  
**C#**  
**[ASP.NET](http://ASP.NET) (Webform)**

---

<div class="post-metadata">

**Author:** ![LawrenceSPK](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@LawrenceSPK](https://discuss.elastic.co/u/LawrenceSPK)\
**Post date:** [May 12, 2019, 5:29pm UTC](https://discuss.elastic.co/t/how-to-bind-searchresult-from-elasticsearch-into-gridview-c-asp-net-by-using-nest/180649/3 "2019-05-12T17:29:47Z")

</div>

I solve my problem already.  
The code below is how to get the searchResult from ElasticSearch and bind the data into Gridview in [ASP.NET](http://ASP.NET) by using NEST.

```auto
public class Address
{
    [Text(Name = "SN")]
    public string SN { get; set; }

    [Text(Name = "JLN")]
    public string JLN { get; set; }
}

protected void BtnSearch_Clicked(object sender, EventArgs e)
{
    string SearchValue = txtSearchValue.Text;
    string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
    string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
    string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];

    var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
        .DefaultIndex("masterlist*");

    var client = new ElasticClient(settings);

    var searchResponse = client.Search<Address>(s => s
            .From(0)
            .Size(100)
            .Query(q => q
                 .QueryString(qs => qs
                    .Query("JLN:\"" + SearchValue + "\"")
                )
            )
        );

    var address = searchResponse.Documents.ToList();

    ESGridview.DataSource = address;
    ESGridview.DataBind();
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 9, 2019, 5:33pm UTC](https://discuss.elastic.co/t/how-to-bind-searchresult-from-elasticsearch-into-gridview-c-asp-net-by-using-nest/180649/4 "2019-06-09T17:33:17Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
