Reputation: 722
I get this error:
Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.
My code:
protected void Button1_Click(object sender, EventArgs e)
{
JobShopEntities job = new JobShopEntities();
GridView1.DataSource = (from x in job.JobDescriptions where (x.Titlu == TextBox1.Text) select x).First();
GridView1.DataBind();
}
I searched a lot for a solution.. From here I got this solution.
Rest of the code on back end in case have something to do with the error.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewEditIndex];
int rowId = Convert.ToInt32(row.Cells[1].Text);
Response.Redirect("~/Administrator/Management/ManageJobs.aspx?id=" + rowId);
}
Upvotes: 2
Views: 1435
Reputation: 148110
You are bind the grid with object as First will give you an object instead of collection whereas the DataSource expect a collection. If you do not need to bind gridview with single record then you can remove the call of First
method and call ToList() to get list of records.
If you need only the first record then you can use Enumerable.Take which will return you IEnumerable<TSource>
GridView1.DataSource = (from x in job.JobDescriptions
where (x.Titlu == TextBox1.Text) select x).Take(1);
Edit as per comments by OP
If you are bind all the record then you should know know many record you have in the table. If record are in thousand then you can think about paging.
If record are in hundreds then you can use the same method using query without where clause in the Page_Load event.
if(!Page.IsPostBack)
{
JobShopEntities job = new JobShopEntities();
GridView1.DataSource = (from x in job.JobDescriptions).ToList();
GridView1.DataBind();
}
Upvotes: 1