2 Replies - 8870 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: 118
  • 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
            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">
<body style="height: 126px">
    <form id="form1" runat="server">
        <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">
            <br /><br />
            <asp:Button ID="Button1" runat="server" Text="Unesi" Height="18px" style="margin-left: 81px" />
        <asp:ListBox ID="ListBox1" runat="server">


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


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: 448
  • View blog
  • Posts: 1,504
  • 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 offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • 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()

Try 'good to check for errors when saving
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