• 0

[LinqtoSQL, ASP.NET 3.5] Does not add new entities ;(.


Question

Hello,

I have a very basic database and I'm using LinqToSQL. The problem is that when I try to add a new entity to the database, it doesn't commit the changes. Here's the code I have:

		try
		{
			Customer cus = new Customer()
			{
				FirstName = txt_Fname.Text,
				LastName = txt_Lname.Text,
				Email = txt_Email.Text,
				EveningPhone = txt_EveningPhone.Text,
				DayPhone = txt_DayPhone.Text,
				Company = txt_Company.Text,
				City = txt_City.Text,
				Country = country_select.Value,
				Fax = txt_Fax.Text,
				PostalCode = txt_POBox.Text,
				Street = txt_Street.Text,
				Password = txt_Password.Text
			};

			DataClassesDataContext db = new DataClassesDataContext();
			db.Customers.InsertOnSubmit(cus);
			db.SubmitChanges();
		}
		catch (Exception exp)
		{
			this.lbl_error.Text = exp.ToString();
		}

It's not throwing any errors, no exceptions ... etc ...

All I did is that I added a new LinqToSQL item to my project, and I threw in the tables I need. Is there anything else I should do? Why it's not adding the customer to the database?

thanks.

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

Try debugging it by placing a breakpoint on the first curly bracket after the TRY and after the CATCH and step through the code....

GE

Link to comment
Share on other sites

  • 0

I never used InsertOnSubmit function so honestly I am not sure what it does, but you can try submitting the changes, after you add your customer to Context collection:

db.Customers.Add(cus);

db.SubmitChanges();

Link to comment
Share on other sites

  • 0
db.Customers.Add(cus);

db.SubmitChanges();

There is no .Add() function anymore ;(. It's .InsertOnSubmit() now.

It's working now, and I have no idea why :-\. Thanks for the help anyway :).

Link to comment
Share on other sites

  • 0
There is no .Add() function anymore ;(. It's .InsertOnSubmit() now.

It's working now, and I have no idea why :-\. Thanks for the help anyway :).

That's weird, Entity Framework is still using Add function. The consistency between Microsoft technologies is quite funny sometimes. Glad to hear you solved your issue.

Link to comment
Share on other sites

  • 0

Lol, dont get me wrong, I much prefer Linq-to-Sql at the moment, but its limited in that it is a Sql-only technology, and is deployment is one way, as in you create your business objects from pre-existing tables, etc. The Entity-framework will eventually be much more platform agnostic, as well as allow you to create databases from your entity model.

Link to comment
Share on other sites

  • 0
Lol, dont get me wrong, I much prefer Linq-to-Sql at the moment, but its limited in that it is a Sql-only technology, and is deployment is one way, as in you create your business objects from pre-existing tables, etc. The Entity-framework will eventually be much more platform agnostic, as well as allow you to create databases from your entity model.

The selling point is LINQ itself to be honest. Linq-to-sql is just a part under the linq umbrella.

Link to comment
Share on other sites

  • 0

^ You can get Linq to Entities ;)

Linq is a layer that sits above your data source, as long as you have access to (or can create) a Query provider, you can pretty much use it to interrogate most things. I'm using it heavily in code these days, and you can performance tune a lot of what you do.

Link to comment
Share on other sites

  • 0
Linq is a layer that sits above your data source, as long as you have access to (or can create) a Query provider, you can pretty much use it to interrogate most things. I'm using it heavily in code these days, and you can performance tune a lot of what you do.

You're correct about Linq to entities, I completely forgot about it. I've only used Entities once in my life, and that was around 2 years ago. It was ... **** :p. Linq-to-sql feels more "integrated".

I'm currently in the process of going 100% linq-to-sql, but it's hard when modifying a project with sql queries everywhere ;(.

Link to comment
Share on other sites

  • 0

Yeah, replacing the plumbing can be quite difficult if you've already invested quite a bit in an existing system. I take it you are not using stored procs with Linq-to-Sql? If not, have you done much performance profiling?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.