2 Replies - 8400 Views - Last Post: 20 March 2013 - 04:56 PM Rate Topic: -----

#1 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Entity FW - Can't insert data after first?

Posted 18 March 2013 - 03:39 AM

Can anyone explain me why this does not work:

First row was inserted normally, but second no. Where is the problem?

ASP.NET code for button click
try
        {
            Radnici r = new Radnici(); //table Radnici means Workers
            r.ime_radnika = TextBox1.Text; //worker name
            r.plata = Convert.ToDecimal(TextBox2.Text); //worker salary 
            r.valuta = DropDownList1.SelectedValue; //currency
            db.Entry(r).State = System.Data.EntityState.Added; //insert data
            db.SaveChanges(); //save changes
            Selektuj(); //select and show data in listbox
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message); //write error
        }



html code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="height: 126px">
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" Height="130px" Width="256px" BorderStyle="Solid" BorderWidth="2px">
            <asp:Label ID="Label1" runat="server" Text="Ime i prezime:"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" style="margin-left: 7px"></asp:TextBox><br />
            <asp:Label ID="Label2" runat="server" Text="Plata:"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 57px" Width="97px"></asp:TextBox><br />
            <asp:Label ID="Label3" runat="server" Text="Plata:"></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server" style="margin-left: 57px">
                <asp:ListItem>DIN</asp:ListItem>
                <asp:ListItem>EUR</asp:ListItem>
                <asp:ListItem>DOL</asp:ListItem>
                <asp:ListItem>YEN</asp:ListItem>
                <asp:ListItem>RUB</asp:ListItem>
                <asp:ListItem>DEM</asp:ListItem>
            </asp:DropDownList>
            <br /><br />
            <asp:Button ID="Button1" runat="server" Text="Unesi" Height="18px" style="margin-left: 81px" />
        </asp:Panel>
    </div>
        <asp:ListBox ID="ListBox1" runat="server">
            <asp:ListItem>

            </asp:ListItem>
        </asp:ListBox>
    </form>
</body>
</html>



mssql code
create table [dbo].[Radnici](
	radnikID int primary key identity not null,
	ime_radnika nvarchar(50),
	plata decimal(18,2),
	valuta nvarchar(20)
)




Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Entity FW - Can't insert data after first?

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Entity FW - Can't insert data after first?

Posted 19 March 2013 - 05:09 AM

What do you mean by row? Looking at your code it looks like you're only trying to insert a single row of data.
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,175
  • Joined: 02-July 08

Re: Entity FW - Can't insert data after first?

Posted 20 March 2013 - 04:56 PM

With EF, after you make the new record for your table you use the AddObject method of the table it belongs to then call SavaChanges()

db.Entry.AddObject(r)
Try 'good to check for errors when saving
 db.SaveChanges()
Catch
End Try


I don't know if Entry is the right table since it is a 'Radnici' record. Usually the record is singular and the table is the plural version of the name for your table.

This post has been edited by _HAWK_: 20 March 2013 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1