Answered by:
Linq insert doesnt save to DB?

Question
-
Hello everyone,
Within my code it is possible to put data into a table, but it isn’t saved. The steps that I make: Through a form I want to add a person. When I add a person the gridview shows the added person, but when I have a look at the database data the person isn’t in it. So I believe my query is talking to a temporary database? My database has also a foreign key relationship, but I guess that wouldn’t make any difference..?
public void InsertPersonInAddressBook(string FirstName, string LastName, DateTime BirthDay) { Person newPerson = new Person(); newPerson.FirstName = FirstName; newPerson.LastName = LastName; newPerson.BirthDay = BirthDay; DBPersonDataContext dc = new DBPersonDataContext(); dc.Persons.InsertOnSubmit(newPerson); dc.SubmitChanges(); } //And gridview refresh method: private void GetUsersIntoTable() { DBPersonDataContext dc = new DBPersonDataContext(); var q = from a in dc.GetTable<Person>() select new { a.PersonID, a.FirstName, a.LastName }; dataGridViewPerson.DataSource = q; }
Any ideas?
Greetings,
SpaceLama
Wednesday, April 25, 2012 8:22 PM
Answers
-
On 4/26/2012 3:05 PM, SpaceLama wrote:> What do you mean by in play? You cant add a person if you dont add an> adress at the same time? Or what are you saying?You are trying to look at this from a SQL Server standpoint and a T-SQLstandpoint, which you need to toss out the door. Linq-2-SQL is usingthis technology behind the scene, but Linq-2-SQL is doing everything foryou as long as you have the object or objects set-up correctly.You need to stop and understand this somewhat, because what you areusing with Linq-2-SQL along with Linq are objects and not data rows andcolumns within rows.Linq is an object oriented programming language. It allows a source suchas a database, array, collections etc etc to be queried with the resultsreturned as an object or a collection of objects in a tubular form.Linq-2-SQL is really an ORM solution Object Relational Mapping solution.AddressBook, Person and Address are setting on a model, and they areobjects.AddressBook is an object that represents a table called AddressBook.Person is an object that represents a table called Person.Address is an object that represents a table called Address.An object can be a parent to children objects, and the parent objectis a container for the children objects.AddressBook object is a container for all Person objects.Person object is a contained for all Address objects.This should have been implemented on the Linq-2-SQL model because of theforeign-key constraint you had on the Person table pointing to theAddressBookID it belongs to in the AddressBook table. The Addressshould have a foreign-key to PersonID in the Person table it belongs to.Looking at from an OOP standpoint, which is how Linq-2-SQL is looking at it.AddressBook object should have a Person object and a Person objectsshould have Address objects.If doing an add for the first time, you could have this.New Address object with 1 New Person object within the AddressBookobject, and one or more new Address objects within the Person object.Save the AddressBook object that had everything in it (the container),and Linq-2-SQL will save it all and make the foreign-key associationsitself.You could have just added a single AddressBook object by itself with noother objects.But to add more Person objects to the AddressBook object, you need tothe get the existing AddressBook object in order to add a new Personobject to it. And then you can save AddressBook, which would have savedthe Person.In order to add more Address records to a Person object, then you willneed to get the existing AddressBook.Person Object in order to add moreAddress objects to the Person object and then save Addressbook theparent container object to a Person object with the Person object beingthe parent container object to an Address object.You need to stop and understand what you are trying to do here and whatyou need to do here in order to accomplish things with Linq-2-SQL,because there are 3 different OOP technologies you are using here, andyou don't understand them.I suggest you go get yourself a good book on Linq-2-SQL, and if you haveone, the start at page one again and do the examples until you hit thelast page. It will come together for you then, because you don'tunderstand what you are doing or how to do it.HTH
- Marked as answer by SpaceLama Sunday, April 29, 2012 6:59 AM
Friday, April 27, 2012 2:34 PM -
99.999% of the time when this happens, it's because you're looking at a different database than your application. Look carefully at the connection string in your config file, and make sure you're looking at that same database. In particular, make sure the file path or server path (depending on what database type you're using) is correct.
Check out My Blog. Now updated to actually work!
- Marked as answer by SpaceLama Sunday, April 29, 2012 6:59 AM
Wednesday, April 25, 2012 9:02 PM -
Hello,
Please follow this link to insert data in database using linq................
http://msdn.microsoft.com/en-us/library/bb386941.aspx
http://stackoverflow.com/questions/3839930/linq-to-sql-insert-data-c-sharp
Regards,
Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights
- Marked as answer by SpaceLama Sunday, April 29, 2012 6:59 AM
Friday, April 27, 2012 4:39 PM
All replies
-
On 4/25/2012 4:22 PM, SpaceLama wrote:> Hello everyone,>> Within my code it is possible to put data into a table, but it isn’t> saved. The steps that I make: Through a form I want to add a person.> When I add a person the gridview shows the added person, but when I have> a look at the database data the person isn’t in it. So I believe my> query is talking to a temporary database? My database has also a foreign> key relationship, but I guess that wouldn’t make any difference..?>> public void InsertPersonInAddressBook(string FirstName, string LastName, DateTime BirthDay)> {> Person newPerson = new Person();> newPerson.FirstName = FirstName;> newPerson.LastName = LastName;> newPerson.BirthDay = BirthDay;> DBPersonDataContext dc = new DBPersonDataContext();> dc.Persons.InsertOnSubmit(newPerson);> dc.SubmitChanges();> }> //And gridview refresh method:> private void GetUsersIntoTable()> {> DBPersonDataContext dc = new DBPersonDataContext();> var q = from a in dc.GetTable<Person>()> select new { a.PersonID, a.FirstName, a.LastName };> dataGridViewPerson.DataSource = q;> }>> Any ideas?>> Greetings,>> SpaceLama>Maybe it's blowing up and you don't know it because you have notry/catch around the code, which you should always have when accessing adatabase.Yeah it does make a difference if there are constrains (relationships)that you are not honoring during an insert or a delete of a tablerow/object ((object) for Linq-2-SQL).There is no temp database. The program is not working and there issomething wrong if data is not saving to the database.Wednesday, April 25, 2012 8:29 PM
-
Are you catching (and ignoring) an exception somewhere? Just because you can create a new person and submit it doesn't mean that the backing database will accept the change. In particular, your db may reject it based on a missing or invalid field or required associated record.
The basic code for InsertPersonInAddressBook looks okay, assuming the default constructor for your DBPersonDataContext gets the right connection string with a parameterless constructor. Maybe that's worth checking too.
I personally don't like creating a new data context every time you want to do a transaction. I like to create one context and reuse it.
Wednesday, April 25, 2012 8:31 PM -
Could u give me some examples?
Thanks for the advice!
Greetings,
Spacelama
Wednesday, April 25, 2012 8:55 PM -
99.999% of the time when this happens, it's because you're looking at a different database than your application. Look carefully at the connection string in your config file, and make sure you're looking at that same database. In particular, make sure the file path or server path (depending on what database type you're using) is correct.
Check out My Blog. Now updated to actually work!
- Marked as answer by SpaceLama Sunday, April 29, 2012 6:59 AM
Wednesday, April 25, 2012 9:02 PM -
On 4/25/2012 4:55 PM, SpaceLama wrote:> Could u give me some examples?>> Thanks for the advice!>> Greetings,>> Spacelama>I suggest that you use and learn form this.If Person is linked to Addressbook and Address is linked to Person, thenwhat you are doing is not right.AdressBook.Persons.AddressesPlural meaning more than one Person (collection) linked to anAddressBook and more than one Address (collection) to a Person.You just can't add a Person object without the AddressBook object beingin scope, and you cannot add and Address object without theAddressBook.Persons objects being in scope.You can insert an AdddressBook my itself. But you cannot insert a Personwithout the Addressbook object being in play. And you cannot add anAddress object unless the Addressbook.Person objects are in play.Addressbook.Persons.Addresses with the plural meaning collections withthe object scheme linked together by associations or foreign keys.Wednesday, April 25, 2012 9:14 PM
-
What do you mean by in play? You cant add a person if you dont add an adress at the same time? Or what are you saying?Thursday, April 26, 2012 7:05 PM
-
On 4/26/2012 3:05 PM, SpaceLama wrote:> What do you mean by in play? You cant add a person if you dont add an> adress at the same time? Or what are you saying?You are trying to look at this from a SQL Server standpoint and a T-SQLstandpoint, which you need to toss out the door. Linq-2-SQL is usingthis technology behind the scene, but Linq-2-SQL is doing everything foryou as long as you have the object or objects set-up correctly.You need to stop and understand this somewhat, because what you areusing with Linq-2-SQL along with Linq are objects and not data rows andcolumns within rows.Linq is an object oriented programming language. It allows a source suchas a database, array, collections etc etc to be queried with the resultsreturned as an object or a collection of objects in a tubular form.Linq-2-SQL is really an ORM solution Object Relational Mapping solution.AddressBook, Person and Address are setting on a model, and they areobjects.AddressBook is an object that represents a table called AddressBook.Person is an object that represents a table called Person.Address is an object that represents a table called Address.An object can be a parent to children objects, and the parent objectis a container for the children objects.AddressBook object is a container for all Person objects.Person object is a contained for all Address objects.This should have been implemented on the Linq-2-SQL model because of theforeign-key constraint you had on the Person table pointing to theAddressBookID it belongs to in the AddressBook table. The Addressshould have a foreign-key to PersonID in the Person table it belongs to.Looking at from an OOP standpoint, which is how Linq-2-SQL is looking at it.AddressBook object should have a Person object and a Person objectsshould have Address objects.If doing an add for the first time, you could have this.New Address object with 1 New Person object within the AddressBookobject, and one or more new Address objects within the Person object.Save the AddressBook object that had everything in it (the container),and Linq-2-SQL will save it all and make the foreign-key associationsitself.You could have just added a single AddressBook object by itself with noother objects.But to add more Person objects to the AddressBook object, you need tothe get the existing AddressBook object in order to add a new Personobject to it. And then you can save AddressBook, which would have savedthe Person.In order to add more Address records to a Person object, then you willneed to get the existing AddressBook.Person Object in order to add moreAddress objects to the Person object and then save Addressbook theparent container object to a Person object with the Person object beingthe parent container object to an Address object.You need to stop and understand what you are trying to do here and whatyou need to do here in order to accomplish things with Linq-2-SQL,because there are 3 different OOP technologies you are using here, andyou don't understand them.I suggest you go get yourself a good book on Linq-2-SQL, and if you haveone, the start at page one again and do the examples until you hit thelast page. It will come together for you then, because you don'tunderstand what you are doing or how to do it.HTH
- Marked as answer by SpaceLama Sunday, April 29, 2012 6:59 AM
Friday, April 27, 2012 2:34 PM -
Hello,
Please follow this link to insert data in database using linq................
http://msdn.microsoft.com/en-us/library/bb386941.aspx
http://stackoverflow.com/questions/3839930/linq-to-sql-insert-data-c-sharp
Regards,
Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights
- Marked as answer by SpaceLama Sunday, April 29, 2012 6:59 AM
Friday, April 27, 2012 4:39 PM