插入,更新和使用Entity Framework删除 [英] Insert, update & delete using Entity Framework

查看:55
本文介绍了插入,更新和使用Entity Framework删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Entity Framework插入数据但是我得到了运行时异常: -

DataSource和DataSourceID都在'GridView1'上定义。删除一个定义。



以下是代码: -

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);
            ae.employees.AddObject(emp);
            ae.SaveChanges();
            Response.Write("data inserted....");
            GridView1.DataSource = ae.employees;
            GridView1.DataBind();
        }




Employee ID:-
        <asp:TextBox ID="TextBox1" runat="server">
    
<p>
        Employee Name:-
        <asp:TextBox ID="TextBox2" runat="server">
    </p>
<p>
        Employee Designation :-
        <asp:TextBox ID="TextBox3" runat="server">
    </p>
<p>
        Employee Salary:-
        <asp:TextBox ID="TextBox4" runat="server">
    </p>
<p>
        <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">
        
     
    </p>
    <p>

        <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" />
            <columns>
                <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" />
            </columns>
            <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">
        
    </p>





&事情是它插入异常,但我不需要任何异常。



& the thing is its inserting with an exception but i don't need any exception.

推荐答案

你指出数据源两次,所以C#不知道哪一个使用。看到您的网格已经绑定在网站中。删除行:



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


这篇关于插入,更新和使用Entity Framework删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆