SubmitChanges not updating the data in the database

HI Experts

I have the following table in DB:

CREATE TABLE [dbo].[vdc_options](
      [option_id] [int] IDENTITY(0,1) NOT NULL,
      [option_name] [nvarchar](50) COLLATE Hebrew_CI_AS NOT NULL,
      [option_value] [nvarchar](50) COLLATE Hebrew_CI_AS NOT NULL)
Where option_name is defined as the primary key of this table

I have defined the following table in my c# class
[Table]
        private class vdc_options
        {
            [Column]
            public int option_id { get; private set; }
            [Column]
            public string option_name { get; set; }
            [Column]
            public string option_value { get; set; }
        }

and now , I am trying to update some of the values  (and I have checked that there are changes)

No Exception is thrown , and the updated values are not saved to the database.

What am i missing???

Thanks




private static bool SaveApplicationSettingsToDB()
            {
                // DataContext takes a connection string. 
                using (DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["VdcDbConnection"].ConnectionString))
                {
                    bool result = false;
                    var options = (from o in db.GetTable<vdc_options>()
                                   select o);
 
                    foreach (var option in options)
                    {
                        option.option_value = VdcAppSettings[option.option_name].Value;
                    }
                    try
                    {
                        db.SubmitChanges();
                        result = true;
                    }
                    catch (ChangeConflictException ex)
                    {
                        MessageBox.Show(string.Format("Failed to save changes {0}", ex.Message));
                    }
                    return result;
                }
 
            }

Open in new window

LVL 19
elimesikaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi elimesika;

In order to update a database the table def needs to indicate which column is the primary key. As you stated in the question that the column option_name is the primary key make the following change to the column attribute.

[Table]
private class vdc_options
{
    [Column]
    public int option_id { get; private set; }
    [Column(CanBeNull=false, IsPrimaryKey=true)]
    public string option_name { get; set; }
    [Column]
    public string option_value { get; set; }
}

Column Attribute Members Documentation
http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute_members.aspx

Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elimesikaAuthor Commented:
Thanks FernandoSoto
Works GREAT!!!
0
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.