Reputation: 115
I want to use an ObjectDataSource with a GridView in ASP.NET. Displaying the data in the GridView works. Now I add a CommandField to the GridView to also enable editing the data. The Update Method works fine, but I have Problems with deleting and inserting:
My ObjectDataSource looks like this:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="[...].TSEndpoint"
DeleteMethod="Remove" InsertMethod="Add"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetTSEndpoints"
TypeName="[...].TSRepository"
UpdateMethod="Update"></asp:ObjectDataSource>
My BusinessObject Manager is TSRepository.cs:
[DataObject]
public class TSRepository : ITSRepository
{
private ISessionFactory _sessionFactory;
private Configuration _configuration;
public TSRepository()
{
_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly(typeof(TSEndpoint).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public void Add(TSEndpoint testSystemEndpoint)
{
if (testSystemEndpoint != null)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(testSystemEndpoint);
transaction.Commit();
}
}
}
[DataObjectMethod(DataObjectMethodType.Update)]
public void Update(TSEndpoint testSystemEndpoint)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Update(testSystemEndpoint);
transaction.Commit();
}
}
[DataObjectMethod(DataObjectMethodType.Delete)]
public void Remove(TSEndpoint testSystemEndpoint)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete(testSystemEndpoint);
transaction.Commit();
}
}
[DataObjectMethod(DataObjectMethodType.Select)]
public ICollection<TSEndpoint> GetTSEndpoints()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var testSystems = session
.CreateCriteria(typeof(TSEndpoint))
.List<TSEndpoint>();
return testSystems;
}
}
}
I would be very glad if someone could help me with my two problems.
Upvotes: 1
Views: 1803
Reputation: 115
Concerning question 2: I still don't know why the InsertMethod is not called. But this workaround is ok for me now: I simply made a form under the GridView to add a new entry.
Upvotes: 0
Reputation: 171
For question 1, I spent the quite a bit of time tracking down a very similar problem in my own code.
For me, the solution was to set the DataKeyNames property in the gridview to the column name of the Primary Key for my object. Once I did this, everything worked perfectly.
Upvotes: 4