Link to home
Create AccountLog in
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

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

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
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
Also How are you determining that the database is not updating?
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! :|
Is the database a local database file located in your project?
the database is our local sql express server
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?
oh yes sorry, yeah the client.dmbl file is in teh App_code folder
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
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

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
thanks
Not a problem, glad to help.