Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is my api code with entity framework..
public class UpdateHotelInfoesController : ApiController
    {
        private UpdateHotelInfoEntities db = new UpdateHotelInfoEntities();

        // GET: api/UpdateHotelInfoes
        public IQueryable<updatehotelinfo> GetUpdateHotelInfo()
        {
            return db.UpdateHotelInfo;
        }

        // GET: api/UpdateHotelInfoes/5
        [ResponseType(typeof(UpdateHotelInfo))]
        public IHttpActionResult GetUpdateHotelInfo(int id)
        {
            UpdateHotelInfo updateHotelInfo = db.UpdateHotelInfo.Find(id);
            if (updateHotelInfo == null)
            {
                return NotFound();
            }

            return Ok(updateHotelInfo);
        }

this is source code

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
            <columns>
                <asp:BoundField DataField="HOTEL_NAME" HeaderText="HOTEL_NAME" />
                <asp:BoundField DataField="ADDRESS_LINE" HeaderText="ADDRESS_LINE" />
                <asp:BoundField DataField="AREA" HeaderText="AREA" />
                <asp:BoundField DataField="CITY" HeaderText="CITY" />
                <asp:BoundField DataField="POSTAL_CODE" HeaderText="POSTAL_CODE" />
                <asp:BoundField DataField="STATE" HeaderText="STATE" />
                <asp:BoundField DataField="COUNTRY" HeaderText="COUNTRY" />
                <asp:BoundField DataField="PHONE_NUMBER" HeaderText="PHONE_NUMBER" />
                <asp:BoundField DataField="LANDLINE" HeaderText="LANDLINE" />
                <asp:BoundField DataField="EMAIL_ID" HeaderText="EMAIL_ID" />
                <asp:BoundField DataField="WEBSITE" HeaderText="WEBSITE" />
                <asp:BoundField DataField="PROPERTY_TYPE" HeaderText="PROPERTY_TYPE" />
                <asp:BoundField DataField="PROPERTY_GRADE" HeaderText="PROPERTY_GRADE" />
                <asp:BoundField DataField="REGISTRATION" HeaderText="REGISTRATION" />
            
        

this is my c# code for displaying.it in grid view


protected async void Button4_Click(object sender, EventArgs e)
        {

          
            up.POSTAL_CODE = Convert.ToInt32(TextBox2.Text);
          
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(Base_URL);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            StringContent con = new StringContent(JsonConvert.SerializeObject(up), Encoding.UTF8, "application/json");



            HttpResponseMessage response = await client.GetAsync("api/UpdateHotelInfoes/" + TextBox2.Text);
            response.EnsureSuccessStatusCode();
            if (response.IsSuccessStatusCode)
            {            
                  var data = await response.Content.ReadAsStringAsync();
                

                    var a = JsonConvert.DeserializeObject<mainobject>(data);
                                   
                         GridView1.DataSource = a;>>>..here am getting exception.
                         GridView1.DataBind();
}
anyone knows how to get rid of this issue?

What I have tried:

i tried using the above code .but its giving the exception  <pre>Data source is an invalid type. It must be either an ilistsource, ienumerable, or idatasource?.
how to get rid of this?
Posted
Updated 19-May-17 2:03am
v5

1 solution

You are trying to bind the datasource to a single class, but a datasource needs a collection of classes. If you want to bind a datasource to a single class you need to make a collection that only has that class in it

GridView1.DataSource = new List<mainobject>{a};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900