简体   繁体   中英

Linq2SQL not updating record on SQL Azure

I am experimenting with Azure and am running into an issue I cannot seem to find an answer to.

I am running a linq query to update Azure Database with changes to a record but for some reason the changes are not being noted or submitted to the database. The code I am running is as follows:

DataClasses1DataContext update = new DataClasses1DataContext();

    Company extra = update.Companies.Single(p => p.ID == Convert.ToInt32(Request.QueryString["ID"]));

    extra.companyName = txtCompanyName.Text;
    extra.address1 = txtAddress1.Text;
    extra.address2 = txtAddress2.Text;
    extra.address3 = txtAddress3.Text;
    extra.address4 = txtAddress4.Text;
    extra.town = txtTown.Text;
    extra.county = txtCounty.Text;
    extra.postCode = txtPostCode.Text;
    extra.billingEmail = txtBillingEmail.Text;
    extra.facebook = txtFacebook.Text;
    extra.fax = txtFaxNumber.Text;
    extra.faxArea = txtFaxArea.Text;
    extra.faxCountry = txtFaxCountry.Text;
    extra.linkedIn = txtLinkedIn.Text;
    extra.mainEmail = txtMainEmail.Text;
    extra.mainPhone = txtMainLineNumber.Text;
    extra.mainPhoneArea = txtMainLineArea.Text;
    extra.mainPhoneCountry = txtMainLineCountry.Text;

    if (drpMarketing.Text == "No")
    {
        extra.marketingYesNo = 0;
    }
    else
    {
        extra.marketingYesNo = 1;
    }

    extra.salesEmail = txtSalesEmail.Text;
    extra.twitter = txtTwitter.Text;
    extra.website = txtWebsite.Text;

    update.SubmitChanges();

What I am noticing by setting break points is that if I load the page and then change some data then the new data is not being reflected when this code is called (it is in a btnUpdate_Click event)

So When the submitChanges() is used then the new data is not present to submit. Any help appreciated.... I think I have looked at this for so long it needs some fresh eyes lol.

Are you sure you are not repopulating your form OnPostback with the original data (re-selected from the database) so that the linq update actually works, but you are always submitting the most recent database data back to the database.

In other words are you sure you are only populating your form

if(!Page.IsPostback)
{
//populate the form
}

this will preserve user amends when a postback happens.

Have you tried to wrap the SubmitChanges in a try block? Maybe it is throwing an exception. Here is how it is documented to do updates which is very similar to what you are doing:

http://msdn.microsoft.com/en-us/library/bb399339.aspx

Also, does this work against a local SQL Server database or SQL Express database?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM