0
votes

I am trying to do an insert using linq to sql but am getting the following error Additional information: Cannot insert the value NULL into column 'UserID', table 'Itiss_Request.dbo.Users'; column does not allow nulls. INSERT fails.

The UserID table is the pk aswel as the identity has been set to autoincrement. The database has 4 fields.

        DataClasses1DataContext dt = new DataClasses1DataContext();
        User usr = new User();
        usr.MudID = a[1];
        usr.Email = Session["email"].ToString();
        usr.Name = Session["userName"].ToString();


        dt.Users.InsertOnSubmit(usr);

        dt.SubmitChanges();

This is an from my context file

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_UserID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] public int UserID { get { return this._UserID; } set { if ((this._UserID != value)) { this.OnUserIDChanging(value); this.SendPropertyChanging(); this._UserID = value; this.SendPropertyChanged("UserID"); this.OnUserIDChanged(); } } }

1

1 Answers

0
votes

Please try this...

    DataClasses1DataContext dt = new DataClasses1DataContext();
    User usr = new User();
    usr.MudID = a[1];
    usr.Email = Session["email"].ToString();
    usr.Name = Session["userName"].ToString();
    dt.Users.AddObject(usr);
    dt.SaveChanges();