Hello Everyone, I'm quite new to Linq and have had some problems updating my dabase.
I don't know if it is actually happening for be using a global data context or I'm missing something.
I have my typed DataContex and one Static Public Var to initialize it located within the namespace AlpaCommon, like following:


***My partial datacontext*******************

// partial datacontext class
namespace
AlpaCommon
{
public partial class AlpaDataContext : System.Data.Linq.DataContext
{


//Insert method is working...
public void InsertAnimal2(Animal instance)
{
Animais.InsertOnSubmit(instance);
SubmitChanges();
} 
 
//Delete method is working...
public void DeleteAnimal2(int animalID)
{
var animal = (from a in Animais where a.AnimalID == animalID select a).First();
Animais.DeleteOnSubmit(animal);
SubmitChanges();
}
 
//Update method IS NOT working...
public void UpdateAnimal2(Animal newAnimal)
{.
var animal = (from a in Animais where a.AnimalID == newAnimal.AnimalID select a).First();
animal = newAnimal;
SubmitChanges();
}

This is where I'm instanciating the datacontext e other variables I'll need in the whole app*************

//global DataContext instance
namespace AlpaCommon
{
public static class Globals
{
public static AlpaDataContext db = new AlpaDataContext();

This is the call to the update method ******************************

....................
....................

using AlpaCommon;
namespace Animais
{
public partial class Altera : System.Web.UI.Page
{
....................
....................
....................
....................
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
//cria um novo Objeto do tipo Animal
Animal animalAltera = new Animal();
//set new values 
animalAltera.AnimalID = Convert.ToInt32(Request.Params["AnimalID"]);
animalAltera.Castrado = CastradoCheckBox.Checked;
animalAltera.DisponivelAdocao = DisponivelCheckBox.Checked;
animalAltera.Adotado = AdotadoCheckBox.Checked;
animalAltera.Nome = NomeTextBox.Text;
animalAltera.Tipo = TipoDropDownList.SelectedValue;
animalAltera.Sexo = SexoDropDownList.SelectedValue;
animalAltera.Descricao = DescricaoTextBox.Text;
animalAltera.Local = LocalTextBox.Text;
animalAltera.Foto = AlteraFoto(); 

AlpaCommon.Globals.db.UpdateAnimal2(animalAltera);

redirect = redirectSucesso;

}
catch
{
redirect = redirectErro;
}
finally
{
Helper.Redirect(redirect);
}
}

******************************************************

I'm not catching any exception, it just does not update the database.
Am I missing something in my updating or calling method?
I'm looking forward for suggestions.

Thank you

Dani AI

Generated

Two separate issues explain why the update never reaches the database in ’s code. Assigning animal = newAnimal only changes the local variable; it does not copy values into the entity instance that the DataContext is tracking. Also, keeping a single static DataContext is fragile for web apps: it caches state, can hold stale objects, and will produce surprising behavior — exactly what warned about.

Two safe patterns to make updates work:

  • Fetch-and-update (recommended): create a short-lived DataContext, load the tracked entity, set the properties that changed, then call SubmitChanges(). This preserves change tracking and updates only the modified fields.
using(var db = new AlpaDataContext())
{
    var dbAnimal = db.Animais.Single(a => a.AnimalID == newAnimal.AnimalID);
    dbAnimal.Nome = newAnimal.Nome;
    dbAnimal.Castrado = newAnimal.Castrado;
    // set other fields that changed
    db.SubmitChanges();
}
  • Attach-as-modified (when you have a detached object): create a new DataContext, attach the detached instance and tell LINQ-to-SQL it’s modified, then submit. Use this carefully — it marks all columns as changed and bypasses original-value concurrency checks.
using(var db = new AlpaDataContext())
{
    db.Animais.Attach(newAnimal, true);
    db.SubmitChanges();
}

Guidance and cautions: always scope a DataContext to a short unit of work (for web apps, per request or per operation) and dispose it (use using). If the table uses optimistic concurrency, prefer fetching the entity and setting only changed properties or provide original values when attaching. For reference, see the DataContext and Attach API docs: DataContext class documentation and Table<T>.Attach method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.