Click here to Skip to main content
15,849,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to insert data using Entity Framework but i m getting an run-time exception :-
"Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."

Here's the code:-
public partial class _Default : System.Web.UI.Page
ankushEntities ae = new ankushEntities();
        employee emp = new employee()

protected void Button1_Click(object sender, EventArgs e)

            emp.empid = int.Parse(TextBox1.Text);
            emp.empname = TextBox2.Text;
            emp.empdesig = TextBox3.Text;
            emp.empsalary = int.Parse(TextBox4.Text);
            Response.Write("data inserted....");
            GridView1.DataSource = ae.employees;

Employee ID:-
        <asp:TextBox ID="TextBox1" runat="server">
        Employee Name:-
        <asp:TextBox ID="TextBox2" runat="server">
        Employee Designation :-
        <asp:TextBox ID="TextBox3" runat="server">
        Employee Salary:-
        <asp:TextBox ID="TextBox4" runat="server">
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Update" />
        <asp:Button ID="Button3" runat="server" Text="Delete" />
        <asp:ScriptManager ID="ScriptManager1" runat="server">

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"      AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="empid" 
            DataSourceID="EntityDataSource1" GridLines="Horizontal">
            <alternatingrowstyle backcolor="#F7F7F7" />
                <asp:BoundField DataField="empid" HeaderText="empid" ReadOnly="True" 
                    SortExpression="empid" />
                <asp:BoundField DataField="empname" HeaderText="empname" 
                    SortExpression="empname" />
                <asp:BoundField DataField="empdesig" HeaderText="empdesig" 
                    SortExpression="empdesig" />
                <asp:BoundField DataField="empsalary" HeaderText="empsalary" 
                    SortExpression="empsalary" />
            <footerstyle backcolor="#B5C7DE" forecolor="#4A3C8C" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <pagerstyle backcolor="#E7E7FF" forecolor="#4A3C8C" horizontalalign="Right" />
            <rowstyle backcolor="#E7E7FF" forecolor="#4A3C8C" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <sortedascendingcellstyle backcolor="#F4F4FD" />
            <sortedascendingheaderstyle backcolor="#5A4C9D" />
            <sorteddescendingcellstyle backcolor="#D8D8F0" />
            <sorteddescendingheaderstyle backcolor="#3E3277" />
        <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
            ConnectionString="name=ankushEntities" DefaultContainerName="ankushEntities" 
            EnableFlattening="False" EntitySetName="employees" EntityTypeFilter="employee">

& the thing is its inserting with an exception but i don't need any exception.
Updated 29-Sep-13 19:26pm
Pradeep Shukla 29-Sep-13 11:23am    
The exception says it all, remove the DataSourceID and there won't be any exception.

1 solution

You are pointing out the datasource twice, so C# doesn't know which one to use. Seeing that you grid is bound already in the website. Remove line:

GridView1.DataSource = ae.employees;
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