none
Linq to Sql: Insert Function RRS feed

  • Question

  • this code works fine with no error but the Student table is not updated in the database .. when i select show table data there is no data in the table.
      void EnterData()
            {
                linkDataContext db = new linkDataContext();
                Student std = new Student();
                std.Id = 7;
                std.Name = "Ahsan";
                db.Students.InsertOnSubmit(std);
                db.SubmitChanges();
            }


    samEE

    Sunday, January 12, 2014 7:33 AM

Answers

  • My problem is solved i am using VS2012 .Net 4.5 and Server Explorer of Visual Studio . I am working with local .mdf file and the problem was that i was not connected to the copy of that .mdf file in the debug folder of project.
    And there was another issue the data was not updated after i close visual studio and the solution to that was changing the "copy to output" option of the database to "do not copy" by right clicking the database.mdf in solution explorer and selecting properties.

    samEE

    Monday, January 13, 2014 3:52 PM

All replies

  • I think you are missing connection to your database .

    How to connect to database


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, January 13, 2014 9:16 AM
  • Hello,

    >>this code works fine with no error but the Student table is not updated in the database .. when i select show table data there is no data in the table.

    It is strange that because with your codes, I did a test and it worked pretty fine as below:

    using (DataClasses1DataContext db = new DataClasses1DataContext())
    
                {
    
                    Student std = new Student();
    
                    std.ID = 1;
    
                    std.Name = "Ahsan";
    
                    db.Students.InsertOnSubmit(std);
    
                    db.SubmitChanges();
    
    
                }
    

    How do you create your Linq2Sql object? And could you please share your table schema?

    I used VS2012, .Net 4.5 and Sqlserver2012.

     You can have a look this article written by Soctt, it has a walkthrough about Linq2Sql.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 13, 2014 9:46 AM
    Moderator
  • My problem is solved i am using VS2012 .Net 4.5 and Server Explorer of Visual Studio . I am working with local .mdf file and the problem was that i was not connected to the copy of that .mdf file in the debug folder of project.
    And there was another issue the data was not updated after i close visual studio and the solution to that was changing the "copy to output" option of the database to "do not copy" by right clicking the database.mdf in solution explorer and selecting properties.

    samEE

    Monday, January 13, 2014 3:52 PM