简体   繁体   中英

linq to sql insert statement not working

I have the following code:

        pillboxDataContext db = new pillboxDataContext();

        userAccount newUser = new userAccount();
        newUser.userName = "test123";
        newUser.userPhone = "1234567890";
        newUser.userEmail = "test@test.com";
        newUser.userPwd = "testpassword";
        newUser.userCreateDate = DateTime.Now;
        newUser.userAccountType = "basic";

        db.users.Add(newUser);
        db.SubmitChanges();

userAccount is my object with properties (that match up with the user table).

I'm confused on the db.users.Add(newUser); line. The .Add is stating: System.Data.LINQ.Table does not contain a definition for 'Add'.

The examples I was following seem to indicate that the .Add should be allowed. I'm new at this so please any advice would be very helpful.

UPDATE:

I changed it to db.users.InsertOnSubmit(newUser); however, I'm still getting an error:

The best overloaded method match for 'System.Data.Linq.Table.InsertOnSubmit(user)' has some invalid arguments

Fix!

After looking at other code samples I figured out that I was assigning my data to the class (userAccount) rather than the table (user).

So I changed: userAccount newUser = new userAccount(); to user newUser = new user(); Then everything worked like I expected.

Thanks for the info!

Yes, Add won't work - I suspect you want InsertOnSubmit .

db.users.InsertOnSubmit(newUser);
db.SubmitChanges();

Your code is:

db.users.InsertAllOnSubmit(newUser);

Jon suggested that your code should be:

db.users.InsertOnSubmit(newUser);

There is an enormous difference between the two. An IDE is very helpful for this sort of thing...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM