Anurag Jain
Anurag Jain

Reputation: 1389

Get the field from the nested array

I am getting response from airline webservice in this variable BookingDetailsEmailRS[] bookingDetailsEmailRS.

I want to get Travelers field in the nested array.
fill this field in my grid view Travelers Name column.

protected void Button1_Click(object sender, EventArgs e)
{
    GatewayBookingClient b = new GatewayBookingClient();

    string emailid = "[email protected]";

    string errorCode = string.Empty;
    string errorAtNode = string.Empty;

    SoapAuthentication soap = new SoapAuthentication();
    soap.UserName = "[email protected]";
    soap.Password = "mob1ss1mo1947";
    BookingDetailsEmailRS[] bookingDetailsEmailRS = b.GetBookingDetails(soap, emailid, out  errorCode, out  errorAtNode);

    for (int i = 0; i < bookingDetailsEmailRS.Length - 1; i++)
    {
        GridView1.DataSource = bookingDetailsEmailRS;
        GridView1.DataBind();
    }  
}

I successfully fetched value from the array but another problem is that I want to bind each value of name variable in the "TravellerName" column in the grid. "TravellerName" column created my own in the grid.

    string name = bookingDetailsEmailRS[i].Travelers[0].FirstName +  
    bookingDetailsEmailRS[i].Travelers[0].MiddleName +  
    bookingDetailsEmailRS[i].Travelers[0].LastName;

Upvotes: 1

Views: 151

Answers (2)

Amol Kolekar
Amol Kolekar

Reputation: 2325

Use Linq IEnumerable to assign those values from object array to GridView as follow.....

GridView1.DataSource = bookingDetailsEmailRS.Select(obj=>obj.Travelers).Select(x=>x.FirstName+x.MiddleName+x.LastName).ToList();

if you are getting only one Travelers in bookingDetailsEmailRS then you can directly write as follow...

GridView1.DataSource = bookingDetailsEmailRS.Select(obj=>obj.Travelers[0].FirstName+obj.Travelers[0].MiddleName+obj.Travelers[0].LastName)).ToList();

EDIT:- If you want all the columns along with above concatnated one then try following...

GridView1.DataSource = bookingDetailsEmailRS.Select(obj=>new {
    Username =obj.Travelers[0].FirstName+obj.Travelers[0].MiddleName+obj.Travelers[0].LastName,
    obj.property1,
    obj.property2
}).TolIst();

Upvotes: 2

felix Antony
felix Antony

Reputation: 1460

You can achieve the code

string name = bookingDetailsEmailRS[i].Travelers[0].FirstName + bookingDetailsEmailRS[i].Travelers[0].MiddleName + bookingDetailsEmailRS[i].Travelers[0].LastName;

by returning dataset from the service. In this case you want to return dataset instead of list, arrays.

If you are returning a List you can get the code by

string name = bookingDetailsEmailRS[i].FirstName + bookingDetailsEmailRS[i].MiddleName + bookingDetailsEmailRS[i].LastName;

because bookingDetailsEmailRS[] is the List/array of BookingDetailsEmailRS class.

Thank you

Upvotes: 0

Related Questions