LINQ to SQL update not working!? :|

awilderbeast
awilderbeast used Ask the Experts™
on
Hi all,

ive tried every example i could find online to try and get this update working, nothing works, the data does not update but there are no errors! :|

can anyone help me out?
as you can see i currently tried the below two methods before posting

Thanks
protected void UpdateProfile(object sender, EventArgs e)
    {
        int ID = Convert.ToInt32(pID.Value);

        ClientDataContext dc = new ClientDataContext();

        ClientCV cvData = dc.ClientCVs.Single(p => p.ID == ID);
        /*
            var GetData = (from c in dc.ClientCVs
                          where c.ID == ID
                            select c).Single();
        
            GetData.Description = txtPersonalProfile.Text;
        */
        cvData.Description = txtPersonalProfile.Text;
        try
            {
                dc.SubmitChanges();
               lblHist.Text = "Personal profile updated!";
            }
            catch (Exception ex)
            {
                lblHist.Text = "Error: " + ex.ToString();
            }
        
        
        upProfile.Update();
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Below answer will help me to find out your problem...

Does it return any record?
>> ClientCV cvData = dc.ClientCVs.Single(p => p.ID == ID);

What is function of this line?
>> upProfile.Update();

Author

Commented:
yes returns records fine

1. lblHist.Text = cvData.ID.ToString(); returned "1"

2. that is to update an  update panel

thanks
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi awilderbeast;

Does the database table ClientCVs have a primary key set on one of its columns. If NO then this is the issue, Linq to SQL will not update tables that do not have a primary set on it.

Fernando
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Fernando SotoRetired
Distinguished Expert 2017

Commented:
Also How are you determining that the database is not updating?

Author

Commented:
hi ferenado, you where right i had forgotten to define a primary key in the sql server, i have done so now and have verified the dbml file has the  primary key on it too

but alas is still fails to update

i know its failing becuase im looking at the sql table and it hasnt changed, also reloading the page which would get all the data again and it hasnt updated

ive checked and its getting the right ID its just not updating the record and giving no details as to why! :|
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Is the database a local database file located in your project?

Author

Commented:
the database is our local sql express server
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Does not answer my question. Lets try it this way. In Visual Studio in the Solution Explorer is there a database file that shows up in the tree?

Author

Commented:
oh yes sorry, yeah the client.dmbl file is in teh App_code folder
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi awilderbeast;

OK I was not aware that this is a Web App. In windows form the Database is copied from the current location in the project to the bin directory depending on the property of the database Copy To Output Directory. which has three values, Do not copy, Copy always and Copy if newer. If set to Copy Always the database in the project are always copied to the bin directory while the program is executing it modifies the db in the bin directory and would always seem as if the database was not getting updated.

But seeming you are on a Web App not sure if it works the same way. Check your bin directory to see if a copy of the DB is there.

Fernando

Author

Commented:
we found the problem, the update code worked fine

i needed to add         if (!IsPostBack) to my page load event

also this copy to output directory, is this used for testing or live apps?

so all the data is copied to the bin directory?
when is it updated?
will it update the sql server?

Thanks
Retired
Distinguished Expert 2017
Commented:
Hi awilderbeast

Glad to hear you got it working.

To your question, "also this copy to output directory, is this used for testing or live apps?" this only happens during development , the IDE environment does this.

To your question, "so all the data is copied to the bin directory?", only in the Development of the application and only those items that are mark to do so, like the option
when is it updated?

To your question, "will it update the SQL server?", The connection string in web.config will tell SQL server where the db file is located.

Fernando

Author

Commented:
thanks
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial