Karthikeyan Muthukumaran Karthikeyan Muthukumaran - 2 months ago 11
ASP.NET (C#) Question

GridView, It must be either an IListSource, IEnumerable, or IDataSource

This is my Source Code

<asp:GridView ID="gveducationInfo" CssClass="footable" runat="server" AutoGenerateColumns="false" Style="max-width: 600px" ShowHeaderWhenEmpty="true" DataKeyNames="Education_ID" OnRowCommand="gveducationInfo_RowCommand">
<Columns>
<asp:BoundField DataField="Education_ID" HeaderText="" ItemStyle-CssClass="hiddenColumn" HeaderStyle-CssClass="hiddenColumn" />
<asp:BoundField DataField="Degree_Type" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Year_Of_Passing" HeaderText="Year Of Passing" />
<asp:BoundField DataField="Institute_Name" HeaderText="Institute Name" />
<asp:BoundField DataField="State_ID" HeaderText="State" />
<asp:BoundField DataField="City_ID" HeaderText="City" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<%--<asp:BoundField DataField="Notes" HeaderText="Notes" />--%>
<asp:ButtonField ButtonType="Image" ImageUrl="~/images/edit16.png" CommandName="EditRow" ItemStyle-CssClass="align-center" />
<asp:ButtonField ButtonType="Image" ImageUrl="~/images/delete16.png" CommandName="DeleteRow" ItemStyle-CssClass="align-center" />
</Columns>
</asp:GridView>


This is my C# coding

protected void butSave_ServerClick(object sender, EventArgs e)
{
Business.ATS.ATS ats = new Business.ATS.ATS();
Data.Education_Info education_Info = new Data.Education_Info();
var state = ats.GetStates();
var city = ats.GetCities();
education_Info = ats.GetEducationInfo(Applicant_ID).SingleOrDefault();
education_Info = new Data.Education_Info();
education_Info.Education_ID = 0;
education_Info.Applicant_ID = Applicant_ID;
education_Info.Name = txtdegreename.Value;
education_Info.Year_Of_Passing = txtyearofpassing.Value;
education_Info.Institute_Name = txtnameofinstitution.Value;
education_Info.City_ID = Convert.ToInt32(ddlcity.SelectedValue);
education_Info.State_ID = Convert.ToInt32(ddlstate.SelectedValue);
education_Info.Degree_Type = Convert.ToInt32(ddldegreetype.SelectedValue);
ats.SaveEducationalInfo(education_Info);
gveducationInfo.DataSource = ats.GetEduInfo().Last();
gveducationInfo.DataBind();
}


this is my entity framwork db code

public IList<Education_Info> GetEduInfo()
{
using (Data.ATSEntities dc = new Data.ATSEntities())
{
var lsteducationinfo = dc.Education_Info.ToList();
return lsteducationinfo;
}
}


when i fill the details in the web page and click save page i need to get last record entered by the user in gridview, bu t igot the error


Data source is an invalid type. It must be either an IListSource,
IEnumerable, or IDataSource.


Anyone Know how to get ride of it??

Answer

The call to Last() here is the problem:

gveducationInfo.DataSource = ats.GetEduInfo().Last();

Delete the call to Last() like this:

gveducationInfo.DataSource = ats.GetEduInfo();

Why?

The GetEduInfo method returns an IList, and an IList implements IEnumerable, which will work with the DataSource. When you called Last(), though, you were no longer passing an IEnumerable but were instead passing the last item of the sequence, which won't work.

If you want to bind a single item to your DataSource, then you will need to wrap it up as an IEnumerable. You can use any of the answers to this Passing a single item as IEnumerable<T>, such as wrapping the single item in an array:

gveducationInfo.DataSource = new Education_Info[] {ats.GetEduInfo().Last()};
Comments