Amin Bakhtiari Far
Amin Bakhtiari Far

Reputation: 156

Linq to ADO Update data does not work

I am using Linq in C# and asp.net to update data fields using method SaveChanges(); but it is not working!!! I Don't know why?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
 private GitamarineEntities context = new GitamarineEntities();
 protected void Page_Load(object sender, EventArgs e)
 {
    var ves = context.tblVessels.First(v => v.vID == 15);
    txtNav.Text = ves.vNavEqpm;
 }
 protected void Button1_Click(object sender, EventArgs e)
 {
    var ves =  context.tblVessels.Single(v => v.vID == 15);
    ves.vNavEqpm = txtNav.Text.Trim();
    context.SaveChanges();
 }
}

Edited and still no luck

public partial class Default2 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    //GitamarineEntities1 contextShow = new GitamarineEntities1();
    //var vesShow = contextShow.tblVessels.First(v => v.vID == 15);
    //txtNav.Text = vesShow.vNavEqpm;
    //contextShow.Dispose();
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    GitamarineEntities1 contextUpdate = new GitamarineEntities1();

    var vesUpdate = contextUpdate.tblVessels.First(v => v.vID == 15);
    vesUpdate.vNavEqpm = txtNav.Text;
    contextUpdate.SaveChanges();
  }
}

Guys I have Changed Coding as above and still it works when I Comment out the Page_Load Coding! but this way user can't see what he/she is changing!!!

Upvotes: 0

Views: 85

Answers (3)

Hadi Mohammadi
Hadi Mohammadi

Reputation: 434

All you need to do , is just checking the IsPostBack in Page_Load. Try this:

        if (!IsPostBack)
        {
           GitamarineEntities1 contextShow = new GitamarineEntities1();
           var vesShow = contextShow.tblVessels.First(v => v.vID == 15);
           txtNav.Text = vesShow.vNavEqpm;
        }

Upvotes: 2

Malek A
Malek A

Reputation: 21

What UpdateModel() is doing exactly? try to comment it out and see if vNavEqpm is updating from the text box value. Another thing to consider, try to re-initialize context again inside button click event and dispose it when you're using it, it's not really recommended to keep the connection alive like this, you're probably locking the retrieved record in PageLoad

Upvotes: 0

CularBytes
CularBytes

Reputation: 10321

Considering that your text does get loaded inside the textbox, I guess there is nothing wrong with the linq to database.

Your button on your HTML page is probably not assigned to your Button_Click Event handler.

Your button should look like this:

 <asp:Button ID="button1" runat="server" Text="Button" OnClick="Button1_Click" />

Make sure it is inside a <form> element that also has the attribute runat="server"

Set a breakpoint on the event handler and see if it gets triggered. If not, then we need more code to fix this issue.

Upvotes: 0

Related Questions