J
Jason James
Hi,
I am trying to figure out if I should be using the RowChanged event of
my tableAdapter or a RowUpdated event of a dataAdapter?
I have used data connection to my database file to create a typed
dataset and table adapters to allow me to load and save data to my
database. However, since there is an opportunity for several people to
be accessing the database at the same time I want to use the @@IDENTITY
value of the most recently inserted recrd to update the PK of the
updated table so that duplication PKs are avoided. I have worked this
way on several other occasions without the use of a tableAdapter and
the dataAdapter has exposed a RowUpdated event that I have used to
retrieve the PK of the newly insert record. However, the tableAdapter
does not appear to work in the same way. It exposes a RowChanged
event, but that appears to return 0 when retrieve the PK from within
it. I have read that I *must* use the open connection for the table
that has just updated, which I believe that I am doing. I have placed
all of my data access code in a partial class of the typed dataset and
yet things don't seem to be working right.
Please see the partial DS class below. Any ideas or solutions to how I
should use the PK from the DB update to modify the PK in the dataset
would be greatly apprciated.
Many thanks, Jason.
using System;
using System.Collections.Generic;
using System.Text;
using Jems_Data_Layer.JemsAccountsDataSetTableAdapters;
using System.Data.OleDb;
namespace Jems_Data_Layer
{
public partial class JemsAccountsDataSet
{
public static JemsAccountsDataSet GetCustomers()
{
tblCustomerTableAdapter taCustomer = new
tblCustomerTableAdapter();
tblPhotographerTableAdapter taPhotographer = new
tblPhotographerTableAdapter();
JemsAccountsDataSet dsCustomers = new
JemsAccountsDataSet();
taPhotographer.Fill(dsCustomers.tblPhotographer);
taCustomer.Fill(dsCustomers.tblCustomer);
return dsCustomers;
}
public static void WriteCustomers(JemsAccountsDataSet
dsCustomers)
{
tblCustomerTableAdapter taCustomer = new
tblCustomerTableAdapter();
tblPhotographerTableAdapter taPhotographer = new
tblPhotographerTableAdapter();
dsCustomers.tabletblPhotographer.RowChanged += new
System.Data.DataRowChangeEventHandler(tabletblPhotographer_RowChanged);
dsCustomers.tabletblCustomer.RowChanged += new
System.Data.DataRowChangeEventHandler(tabletblCustomer_RowChanged);
taPhotographer.Update(dsCustomers.tblPhotographer.Select("", "",
System.Data.DataViewRowState.Added|
System.Data.DataViewRowState.ModifiedCurrent));
}
static void tabletblCustomer_RowChanged(object sender,
System.Data.DataRowChangeEventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}
static void tabletblPhotographer_RowChanged(object sender,
System.Data.DataRowChangeEventArgs e)
{
if (e.Action == System.Data.DataRowAction.Add)
{
Jems_Data_Layer.JemsAccountsDataSet.tblPhotographerRow
myRow = (Jems_Data_Layer.JemsAccountsDataSet.tblPhotographerRow)e.Row;
tblPhotographerTableAdapter taPhotographer = new
tblPhotographerTableAdapter();
OleDbConnection conn = taPhotographer.Connection;
OleDbCommand cmdGet = new OleDbCommand(@"SELECT
@@IDENTITY",conn);
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
Int32 id = Convert.ToInt32(cmdGet.ExecuteScalar());
conn.Close();
myRow.pID = id;
myRow.AcceptChanges();
}
}
}
}
I am trying to figure out if I should be using the RowChanged event of
my tableAdapter or a RowUpdated event of a dataAdapter?
I have used data connection to my database file to create a typed
dataset and table adapters to allow me to load and save data to my
database. However, since there is an opportunity for several people to
be accessing the database at the same time I want to use the @@IDENTITY
value of the most recently inserted recrd to update the PK of the
updated table so that duplication PKs are avoided. I have worked this
way on several other occasions without the use of a tableAdapter and
the dataAdapter has exposed a RowUpdated event that I have used to
retrieve the PK of the newly insert record. However, the tableAdapter
does not appear to work in the same way. It exposes a RowChanged
event, but that appears to return 0 when retrieve the PK from within
it. I have read that I *must* use the open connection for the table
that has just updated, which I believe that I am doing. I have placed
all of my data access code in a partial class of the typed dataset and
yet things don't seem to be working right.
Please see the partial DS class below. Any ideas or solutions to how I
should use the PK from the DB update to modify the PK in the dataset
would be greatly apprciated.
Many thanks, Jason.
using System;
using System.Collections.Generic;
using System.Text;
using Jems_Data_Layer.JemsAccountsDataSetTableAdapters;
using System.Data.OleDb;
namespace Jems_Data_Layer
{
public partial class JemsAccountsDataSet
{
public static JemsAccountsDataSet GetCustomers()
{
tblCustomerTableAdapter taCustomer = new
tblCustomerTableAdapter();
tblPhotographerTableAdapter taPhotographer = new
tblPhotographerTableAdapter();
JemsAccountsDataSet dsCustomers = new
JemsAccountsDataSet();
taPhotographer.Fill(dsCustomers.tblPhotographer);
taCustomer.Fill(dsCustomers.tblCustomer);
return dsCustomers;
}
public static void WriteCustomers(JemsAccountsDataSet
dsCustomers)
{
tblCustomerTableAdapter taCustomer = new
tblCustomerTableAdapter();
tblPhotographerTableAdapter taPhotographer = new
tblPhotographerTableAdapter();
dsCustomers.tabletblPhotographer.RowChanged += new
System.Data.DataRowChangeEventHandler(tabletblPhotographer_RowChanged);
dsCustomers.tabletblCustomer.RowChanged += new
System.Data.DataRowChangeEventHandler(tabletblCustomer_RowChanged);
taPhotographer.Update(dsCustomers.tblPhotographer.Select("", "",
System.Data.DataViewRowState.Added|
System.Data.DataViewRowState.ModifiedCurrent));
}
static void tabletblCustomer_RowChanged(object sender,
System.Data.DataRowChangeEventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}
static void tabletblPhotographer_RowChanged(object sender,
System.Data.DataRowChangeEventArgs e)
{
if (e.Action == System.Data.DataRowAction.Add)
{
Jems_Data_Layer.JemsAccountsDataSet.tblPhotographerRow
myRow = (Jems_Data_Layer.JemsAccountsDataSet.tblPhotographerRow)e.Row;
tblPhotographerTableAdapter taPhotographer = new
tblPhotographerTableAdapter();
OleDbConnection conn = taPhotographer.Connection;
OleDbCommand cmdGet = new OleDbCommand(@"SELECT
@@IDENTITY",conn);
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
Int32 id = Convert.ToInt32(cmdGet.ExecuteScalar());
conn.Close();
myRow.pID = id;
myRow.AcceptChanges();
}
}
}
}