none
how use data context on modify record RRS feed

  • Question

  • I am new to work with linq to sql in visual studio.net 2010. I was wondering if you can show me an example of the following:

    1. I would like to find one selected record from a database table using the data context object for linq to sql. (That probably would be using the .first method.)

    2. However, what I would like to know is how to modify a record and have the changes apply to the record in the database.

     The problem I am having is with the DataContext.SubmitChanges(); is not applying the changes. 

    In comparison when you do an insert to the database, you do InsertOnSubmit followed by SubmitChanges(); Also when you do a delete of a record from the database you do deleteOnSubmit followed by SubmitChanges();  However when I just need to do an update to a database record, I am guessing there is a similar a like ChangeonSubmit statement that is followed by submitchanges.

    The following is an example of part of the code I have right now:
                 AtDataContext attDataContext = new AtDataContext();
                 Att att = null;
                 att = new Att();
                 att.Att_id = intQuantity;
                 att.Prior_Cutoff_Date = hold_date;          
                 attDataContext.SubmitChanges();

    Friday, November 18, 2011 11:11 PM

Answers

  • If you have your result already stored in detailsview (which is the same as datagridview?)

    then it is something like that to fill the grid

    Dim att = From c In AtDataContect.customer Select c
    
    me.datagridview.DataSource = att
    
    ' do some changes in the grid
    
    attDataContext.SubmitChanges()
    
    
    



    Monday, November 21, 2011 3:50 PM

All replies

  • No. You had to query and fetch which row you want to modify. For example:

    Get the customer where his id is = to 1000 (I suppose there is only 1 customer)

    Dim att = (from c in AtDataContect.customer where c.id = 1000 select c).first

    att._id = intQuantity
    ....

    ....

    attDataContext.SubmitChanges()

     


    Saturday, November 19, 2011 2:15 PM
  • Hi windsordrive;

    Adding to Samir's post the First method in this statement:

    Dim att = (From c In AtDataContect.customer Where c.id = 1000 Select c).First

    Will throw an exception if no records are found with the id of 1000. To avoid this exception use the FirstOrDefault method in place of First then you can check for Nothing to see if it had found the record.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, November 19, 2011 3:15 PM
  • I know that I have the record I am looking for already since I can see the columns I am working with in a detailsview control. Basically in the application I am working with, the records are brought into a detailsview control using a linq to sql datasource. The user changes some of the values when appropriate and then I want to update the data in the detailsview control using the same linq datasource that I started with.

    What do you suggest I do?

    Sunday, November 20, 2011 7:34 PM
  • Hi windsordrive,

    If you want to the detailsview present the new record, after updating the database, you should to rebind the detailsview:

    DetailsView.Datasource = ... ;

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 21, 2011 8:08 AM
    Moderator
  • If you have your result already stored in detailsview (which is the same as datagridview?)

    then it is something like that to fill the grid

    Dim att = From c In AtDataContect.customer Select c
    
    me.datagridview.DataSource = att
    
    ' do some changes in the grid
    
    attDataContext.SubmitChanges()
    
    
    



    Monday, November 21, 2011 3:50 PM