tags:

views:

511

answers:

1

Hello all, I have a table with names 'Employee' with four field EmployeeId,Name,Address,Age I have set the EmployeeId is Primary key.I want that EmployeeId field will be incremented automatically whenever adding any new records.

Codebehind for insert is: { DataClassesDataContext db = new DataClassesDataContext();

    Employee emp = new Employee {Name = "James", Address = "India", Age = 24};
    db.Employees.InsertOnSubmit(emp);
    db.SubmitChanges();
    ShowEmployee();

}

designer.cs:

[Column(Storage = "_EmployeeId", DbType = "Int NOT NULL", IsPrimaryKey = true,IsDbGenerated=true,CanBeNull=false)]

When I am running this application getting the following error:

Cannot insert the value NULL into column 'EmployeeId', table 'Habib.dbo.Employee'; column does not allow nulls. INSERT fails. The statement has been terminated.

Any help is highly appreciated.

Thanks, Masum

+3  A: 

The EmployeeId field should be an Identity column in your database table. It will automatically increment if you do that. LINQ should pick up that it's an Identity column after that.

mc2thaH
Hi.....in database table how to make identity column any idea?I have created a column name EmployeeId and marked as primary key.
Masuma Aktar
If you are working in SQL Server Management Studio, it will be in the Properties section for the table when in Design mode, the property is: "Identity Column", and you will choose your EmployeeId column from the list.
mc2thaH