Reputation: 567
i am using entity frame work and want to bind data on Grid View but facing problem i have code that i am pasting as well as attaching screen shot i also saw answer regarding this problem but not beneficial for me so any one have experience with this error must be appreciated.
aspx.cs Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
lblMessage.Text = "";
}
void BindGrid()
{
using (GapEntities1 context = new GapEntities1())
{
if (context.Organizations.Count() > 0)
{
// GdvOrganization is a gridview ID name
GdvOrganization.DataSource = context.Organizations;
GdvOrganization.DataBind();
}
}
}
Upvotes: 2
Views: 3645
Reputation: 10565
Seems like you are trying to bind the GridView from Markup side (.aspx ) as well as using code behind.( .aspx.cs )
Choose any one way only to bind the grid.
1.) If you bind gridview from code behind then remove the DataSourceId
property from grid view from markup. Change below code:
<asp:gridview id="GdvOrganization" runat="server" autogeneratecolumns="False"
DataSourceID="MyDataSource">
to
<asp:gridview id="GdvOrganization" runat="server" autogeneratecolumns="False">
2.) if you prefer to bind from markup side then you have to remove the c# code to bind the grid.
Still if above 2 steps doesn't interest you, try below trick ( Recommended ?? )
GdvOrganization.DataSource = ds;
GdvOrganization.DataSourceID = String.Empty;
GdvOrganization.DataBind();
Upvotes: 4