Avatar of awilderbeast
awilderbeastFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

LINQ to SQL update not working!? :|

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

.NET ProgrammingC#

Avatar of undefined
Last Comment
Fernando Soto
Avatar of prajapati84
prajapati84
Flag of India image

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();
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

yes returns records fine

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

2. that is to update an  update panel

thanks
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Also How are you determining that the database is not updating?
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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! :|
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Is the database a local database file located in your project?
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

the database is our local sql express server
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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?
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

oh yes sorry, yeah the client.dmbl file is in teh App_code folder
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

thanks
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Not a problem, glad to help.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo