user1921145
user1921145

Reputation:

Insertonsubmit method asp.net

I want insert a row in my table with linq in asp.net, and I've some troubles with that. I search a lot about this and I found the same solution in everywhere:

context.entity.InsertOnSubmit(variable);

So, I haven't the InsertOnSubmit method, I need do something special to use it? I have using System.Web.Linq.

To overtake this problem I tried with:

context.entity.Add(variable);
context.SaveChanges();

And the result was an error like:

"An error occurred while updating the entries. See the inner exception for details."
DETAILS: "Invalid object name 'ConnectionStringModelStoreContainer.user'."

What do you suggest?

Upvotes: 0

Views: 1159

Answers (3)

user1921145
user1921145

Reputation:

I found the solution of my problem.

In this article explain exactly what I need to do: http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx?Redirected=true

Upvotes: 0

Raja Nadar
Raja Nadar

Reputation: 9499

  1. InsertOnSubmit is a Linq2Sql concept to add rows to the database. SubmitChanges is used to save the changes.
  2. Entity.Add is an entity framework concept. Save changes is used.

In your case, it looks like you're using Entity Framework.

Check your EDMX file and database to see any table mismatches.

Specifically,

  1. go to your connection string (in config or better would be via breakpoint)
  2. Log in to that SQL Server using that exact connection string
  3. Verify if the user table exists there.

Upvotes: 1

jensendp
jensendp

Reputation: 2135

When using Linq2SQL, you will need to access the table you are trying to insert into with context.TableName.InsertOnSubmit(value) where TableName is the name of the table in your dbml file and the value is an isntance of that class that was created for you. Once you have called the InsertOnSubmit() method, you need to call the context.SubmitChanges() method to actually save it to the table. Also, depending on how you created your dbml file, the table name on the context my be plural. ex: For a User table, it may look like context.Users.InsertOnSumbit(user);

Upvotes: 0

Related Questions