none
LINQ to SQL: Not Submitting Changes to the DataBase RRS feed

  • Question

  • Hey,

    I've written an app to retrieve a few things from an SQL table (agentName & agentGUID). From these i need to write the agentName value into the loginName field in the Table. The loginName must correspond with the agentGUID. It all seems to be working however it is not saving and i cannot work out why. Any ideas??

     

    public static void writeUserName(decimal agentGUID, string agentName)
            {
                DataClasses1DataContext dc = new DataClasses1DataContext();
                Table<userLogon> userLogonTable = dc.GetTable<userLogon>();
    
                var userLogonQuery =
                    from userLogons in userLogonTable
                    where userLogons.agentGuid == agentGUID
                    select userLogons;
    
                Console.WriteLine("Query Complete");
    
                foreach (userLogon userLogons in userLogonQuery)
                {
                    Console.WriteLine("Writing Variable");
    
                    userLogons.loginName = agentName;
                    userLogons.logonId = "apple";
                }
    
                try
                {
                    Console.WriteLine("Before Save");
    
                    dc.SubmitChanges();
    
                    Console.WriteLine("After Save");
                    Console.ReadLine();
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
    

    • Moved by Leo Liu - MSFT Monday, December 12, 2011 8:52 AM Moved for better support. (From:Visual C# General)
    Friday, December 9, 2011 6:21 AM

Answers

  • Hi,

    It's not relevant to post this in the Kaseya forums as this would occur on any DB with a table that does not have a PK. I have ended up going through a different route and have used SqlCommand to directly insert a TSQL Query into the DB which is working perfectly. Thanks for all your help!

    Thursday, December 15, 2011 4:05 AM

All replies

  • I added dc.userLogons.InsertAllOnSubmit(userLogonQuery); however i get an error saying that im unable to create update or delete as the Table has no PK. Is there any other way that I'm able to do this?? As i can manually go in and edit the table thus one would come to the conclusion that you would programatically do it.

     

    Any ideas??

    Friday, December 9, 2011 6:27 AM
  • On 12/9/2011 1:27 AM, Brendan Thompson wrote:
    > I added *dc.userLogons.InsertAllOnSubmit(userLogonQuery);* however i get
    > an error saying that im unable to create update or delete as the Table
    > has no PK. Is there any other way that I'm able to do this?? As i can
    > manually go in and edit the table thus one would come to the conclusion
    > that you would programatically do it.
    >
     
    Put an Identity column on the table, name it ID, set it to increment by
    1 each time a row is added to the table and set it to be the primary
    key.  You may not use the primary key for anything, but it satisfies the
    condition, that Linq-2-SQL wants.
     
     
    Friday, December 9, 2011 1:56 PM
  • Hi Brendan Thompson,

     

    if you will insert a data to database u need to have a Primary Key, in your example it show's that u haven't add a PK...

     

    your problem would be answer if you do this ff step,

     

    userLogon Table

     

    1. Insert a column name Id

    2. Data type of Int

    3. In the column property find Identity Specification and Expand.

    4. And select the Yes option in (Is Identity)

    5. Save and try

     

     

    Please send us feedback whatever happens...

     

    Tuesday, December 13, 2011 12:18 PM
  • I cannot create an ID column, however i might be able to assign the GUID column to be a PK without doing any damage. So i will give that a go and see what happens, but there is a possibility that it could break things. I'm doing this on the Kaseya Database
    Wednesday, December 14, 2011 3:14 AM
  • Hi Brendan,

    Welcome!

    Any updates for your testing. Not sure about Kaseya Database, I think should repost here: http://community.kaseya.com/xsp/f/default.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 14, 2011 8:27 AM
    Moderator
  • Hi,

    It's not relevant to post this in the Kaseya forums as this would occur on any DB with a table that does not have a PK. I have ended up going through a different route and have used SqlCommand to directly insert a TSQL Query into the DB which is working perfectly. Thanks for all your help!

    Thursday, December 15, 2011 4:05 AM