Reputation: 921
Hi i am working in MVC3 and for database communication i am using NHIBERNATE
I am getting a problem in updating a record.
Firstly session.SaveorUpdate does not work Then i tried the following thing but this also does not work:(
public bool EditParentStudent(ParentStudent parentstudent)
{
log.Debug("Start");
if (parentstudent == null)
{
throw new ArgumentNullException("parentstudent");
}
ISession session = DataAccessLayerHelper.OpenWriterSession();
ITransaction transaction = session.BeginTransaction();
bool saved = false;
try
{
session.SaveOrUpdate(parentstudent);
transaction.Commit();
saved = true;
}
catch (SessionException ex)
{
if (transaction != null && transaction.IsActive)
transaction.Rollback();
log.Error(ex);
}
finally
{
if (transaction != null)
transaction.Dispose();
if (session != null && session.IsConnected)
session.Close();
}
log.Debug("End");
return saved;
}
Upvotes: 0
Views: 1652
Reputation: 1583
If your entity is persistent you don't need to update it explicitly.
using (var session = sessionFactory.OpenSesion())
using (var tx = session.BeginTransaction())
{
// perform your insert here
tx.Commit();
}
Upvotes: 2
Reputation: 472
Assuming that your mapping is ok, if you are using mvc3 then you should put code inside controller, for example
public ActionResult Edit(ParentStudent parentstudent)
{
//open session
// open transaction
//found existing data
var data = session.Query<ParentStudent>().Where(x=>x.Id == parentstudent.Id).FirstOrDefault();
session.SaveOrUpdate(data);
transaction.Commit();
//close transaction
//close session
return View();
}
maybe is better to put this code inside try catch block and to catch possible exception, but I was trying to make it simple as possible.
Hope this helps.
Upvotes: 0