Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SubmitChanges not updating the data in the database

Posted on 2008-11-06
3
Medium Priority
?
1,810 Views
Last Modified: 2013-11-11
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

0
Comment
Question by:elimesika
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 22896156
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
 
LVL 19

Author Closing Comment

by:elimesika
ID: 31513845
Thanks FernandoSoto
Works GREAT!!!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22897119
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question