RowChanged or RowUpdated

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();
}
}

}

}
 
J

Jim Rand

You probably have discovered that taCustomer does not expose any events.
However, if you expose the underlying dataAdapter of taCustomer, the events
are available.

namespace Jems_Data_Layer.JemsAccountsDataSetTableAdapters
{
public partial class tblCustomerTableAdapter{
public System.Data.SqlClient.SqlDataAdapter ExposedDataAdapter
{
get
{
this.Adapter.SelectCommand = this.CommandCollection[0];
return this.Adapter;
}
}
}
}

Now you can do something like this:

taCustomer.ExposedDataAdapter.RowUpdate += .......
 
J

Jason James

Jim said:
You probably have discovered that taCustomer does not expose any events.
However, if you expose the underlying dataAdapter of taCustomer, the events
are available.

namespace Jems_Data_Layer.JemsAccountsDataSetTableAdapters
{
public partial class tblCustomerTableAdapter{
public System.Data.SqlClient.SqlDataAdapter ExposedDataAdapter
{
get
{
this.Adapter.SelectCommand = this.CommandCollection[0];
return this.Adapter;
}
}
}
}

Now you can do something like this:

taCustomer.ExposedDataAdapter.RowUpdate += .......

Jim,

thanks for the insight. Do you have any more details about what you
are suggesting. I've not a lot of experience in the sort of things you
are talking about.

Many thanks,

Jason.
 
J

Jason James

Jim,

I think I figured it out. Took some doing, but once I had exposed the
data adapters I was able to add the RowUpdated event handler to the
dataset partial class and all worked really well. Many thanks for
pointing me in the right direction. I may move the RowUpdated to the
table adapter partial class to keep the DS stuff and the TA/DA stuff in
their own files, but I atleast have exactly what I was after.

Once again many thanks.

Jason.
 
J

Jim Rand

Thank you. I'm glad I could be of help.

Now take a look at generics - a list of ordered DataAdapters (ordered by
table relationship) that you can pass to a static helper class (reusable) to
handle fills and updates.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top