1
Reply

How to set the auto increment value=true

India Programmer

India Programmer

Jan 14 2009 11:32 AM
7.9k
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.

Answers (1)