By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Custom Event for DataSet Read?

P: n/a

I have a static, thread-safe class with a DataSet as a property.
When the class is instanced into an object, some callers add rows to a
DataTable in the DataSet. Other callers read the DataSet.

When any caller reads the DataSet, I want the object to delete all of
the rows in the DataTable. I think I want to make a custom event but
I don't know where/how to get started. Help?

Thanks.

Nov 15 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"localhost" <pr*******@cohort.ces> wrote in message
news:cl********************************@4ax.com...

I have a static, thread-safe class with a DataSet as a property.
When the class is instanced into an object, some callers add rows to a
DataTable in the DataSet. Other callers read the DataSet.

When any caller reads the DataSet, I want the object to delete all of
the rows in the DataTable. I think I want to make a custom event but
I don't know where/how to get started. Help?


If it is up to me, I would hide the dataset property, and make it accessible
only from my own defined methods...

for example:

public class CustomDataSet
{
private System.Data.Dataset _myDataset;
public CustomDataSet()
{
myDataSet = new System.Data.DataSet( );
}

// One example for a public function might be:
public bool FillDataSet(System.Data.DataAdapter adapter)
{
// Do whatever you wish here..
// ....
return false;
}

public DataRow GetRow(int index)
{
DataRow ret = _myDataset.Tables[ 0 ].Rows[ index ];
// Do whatever you wish here, for example:
_myDataset.Tables[ 0 ].Rows.Clear( );
return ret;
}
}

Or you might find it more robust to define class indexers for getting rows
at certain indecies, while you will still be able to control access to your
hidden dataset.


Nov 15 '05 #2

P: n/a

That is not quite what I am talking about. Thanks for the response,
though.

I'm not doing anything with DataAdapters or anything like that.

What I need to accomplish is a way for the private DataSet, which
exists as a property on the static class (accessed through a public
"get"), to be recreated each time a caller gets an instance of the
class so each caller gets a new DataSet.

Thanks.

On Wed, 25 Feb 2004 23:09:43 +0200, "hOSAM"
<bk*********************@yahoo.com> wrote:

"localhost" <pr*******@cohort.ces> wrote in message
news:cl********************************@4ax.com.. .

I have a static, thread-safe class with a DataSet as a property.
When the class is instanced into an object, some callers add rows to a DataTable in the DataSet. Other callers read the DataSet.

When any caller reads the DataSet, I want the object to delete all of the rows in the DataTable. I think I want to make a custom event but I don't know where/how to get started. Help?
If it is up to me, I would hide the dataset property, and make it

accessibleonly from my own defined methods...

for example:

public class CustomDataSet
{
private System.Data.Dataset _myDataset;
public CustomDataSet()
{
myDataSet = new System.Data.DataSet( );
}

// One example for a public function might be:
public bool FillDataSet(System.Data.DataAdapter adapter)
{
// Do whatever you wish here..
// ....
return false;
}

public DataRow GetRow(int index)
{
DataRow ret = _myDataset.Tables[ 0 ].Rows[ index ];
// Do whatever you wish here, for example:
_myDataset.Tables[ 0 ].Rows.Clear( );
return ret;
}
}

Or you might find it more robust to define class indexers for getting rowsat certain indecies, while you will still be able to control access to yourhidden dataset.


Nov 15 '05 #3

P: n/a

Hi localhost,

If so, can't you create a new dataset in your property's "get" method? Like
this:

public class yourclass
{
private DataSet _ds;

public DataSet dataset
{
get
{
if(something you want)
{
DataSet ds=new DataSet();
//do something to your new dataset
return ds;
}

}
set
{
_ds=value;
}
}
}

In this solution, your public "dataset" property will create a new dataset
when some condition meets.

=========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4

P: n/a

The problem is that the class is instanced only once (Singleton). If
I just use a classic get/set, then a client could populate the DataSet
property and then crash, and then the next client could populate the
DataSet property and get both client DataSet info.

What I need to do is make sure the DataSet is truly new each time a
client instances the object.

I think I need to rework this into two classes, an event raiser and an
event consumer, so each time a client connects an event is raised and
the DataSet property is set to a new one.
How can I do that?

On Fri, 27 Feb 2004 09:04:21 GMT, v-*****@online.microsoft.com
(""Jeffrey Tan[MSFT]"") wrote:

Hi localhost,

If so, can't you create a new dataset in your property's "get" method? Likethis:

public class yourclass
{
private DataSet _ds;

public DataSet dataset
{
get
{
if(something you want)
{
DataSet ds=new DataSet();
//do something to your new dataset
return ds;
}

}
set
{
_ds=value;
}
}
}

In this solution, your public "dataset" property will create a new datasetwhen some condition meets.

=========================================
Thank you for your patience and cooperation. If you have any questions orconcerns, please feel free to post it in the group. I am standing by to beof assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no

rights.

Nov 15 '05 #5

P: n/a

Hi localhost,

Thanks very much for your feedback.

Sorry, I think I did not fully understand your point.

Actually, if you use the sample code I provide, whenever you visit the
dataset property, a new dataset will return. Event you use the same class
instance, each time you invoke this property, a new dataset will return, so
I can not find any problem.

Can you show me the problem?

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #6

P: n/a
/*

Below is a stripped-down version of the class.
It is named singlestonstuff.cs

The usage pattern is:
1. Callers try to get the single instance of the class.
2. The class sets its DataSet property to new.
3. Caller makes multiple method calls to add info to the
DataSet property.
4. Caller requests the entire populated DataSet.
5. Caller disconnects.

I need to guarantee that every time (1) happens,
(2) also happens. Right now, (1) can happen and then
the caller can exit (from a crash, etc), and then when
a new caller appears they will get the same DataSet
populated by a earlier caller.

Is it possible for you to show how a delegate (with
events) can be hooked into this so the DataSet is
always given fresh for each caller?
Right now my workaround is for each caller to
get an instance of the class and then touch the
DsClear() method. I want DsClear to happen
internally with this class instead - can you
please show how this can be done with an event?

This compiles with
"csc /target:library singletonstuff.cs"

Thanks.

*/
using System;
using System.Data;
using System.Collections;

public sealed class DataSetStuffer
{
// Thread-safe usage without using locks
static readonly DataSetStuffer dssInstance =
new DataSetStuffer();

// Explicit static constructor tells compiler
// not to mark type as BeforeFieldInit
// Thanks to Jon at http://www.yoda.arachsys.com/
static DataSetStuffer()
{
}
DataSetStuffer()
{
PrepDs();
}
public static DataSetStuffer GetInstance()
{
return dssInstance;
}
private DataSet _dataSet = new DataSet();
public DataSet dataSet
{
get
{
DataSet outSet = _dataSet.Copy();
DsClear();
return outSet;
}
}
private void PrepDs()
{
string callerName =
new System.Diagnostics.StackFrame(3).GetMethod().Name;
DataTable dataTable = new DataTable();
dataTable.Columns.Add( "Something" ,
typeof(System.String) );
_dataSet.Tables.Add( dataTable );
}
public void TakeInfo( string clientInfo )
{
_dataSet.Tables[0].Rows.Add
( new Object[] { clientInfo } );
}
public void DsClear()
{
_dataSet = new DataSet();
PrepDs();
}
}

Nov 15 '05 #7

P: n/a
Hi localhost,

Thanks very much for your feedback.

I have seen your code, also have seen your problem.

I think your problem may only occur under multithread environment, at the
sequence below:

Thread1: DataSet outSet = _dataSet.Copy();
Thread1: Collapse, or sleep etc..
Thread2: DataSet outSet = _dataSet.Copy();
Thread2: DsClear();
Thread2: return outSet;

Then, thread2 will get the same instance as thread1.

I think the problem is due to the 2 statements below did not executive as
an atom operation
DataSet outSet = _dataSet.Copy();
DsClear();

The solution is make these 2 statements into an atom operation to make
threads access this section synchronously.
In .Net, you can use System.Threading.Mutext class to do synchronization,
like this:

private static Mutex mut = new Mutex();

private DataSet _dataSet = new DataSet();
public DataSet dataSet
{
get
{
mut.WaitOne();
DataSet outSet = _dataSet.Copy();
DsClear();
mut.ReleaseMutex();
return outSet;
}
}

===============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #8

P: n/a

That looks good. Yes, this stand-alone DLL will be used by ASP.NET
callers, so it must be fully thread safe and support multithreading.

Is try...finally functionality necessary inside the get?

Thanks.

On Wed, 03 Mar 2004 02:45:51 GMT, v-*****@online.microsoft.com
(""Jeffrey Tan[MSFT]"") wrote:
Hi localhost,

Thanks very much for your feedback.

I have seen your code, also have seen your problem.

I think your problem may only occur under multithread environment, at thesequence below:

Thread1: DataSet outSet = _dataSet.Copy();
Thread1: Collapse, or sleep etc..
Thread2: DataSet outSet = _dataSet.Copy();
Thread2: DsClear();
Thread2: return outSet;

Then, thread2 will get the same instance as thread1.

I think the problem is due to the 2 statements below did not executive asan atom operation
DataSet outSet = _dataSet.Copy();
DsClear();

The solution is make these 2 statements into an atom operation to makethreads access this section synchronously.
In .Net, you can use System.Threading.Mutext class to do synchronization,like this:

private static Mutex mut = new Mutex();

private DataSet _dataSet = new DataSet();
public DataSet dataSet
{
get
{
mut.WaitOne();
DataSet outSet = _dataSet.Copy();
DsClear();
mut.ReleaseMutex();
return outSet;
}
}

===============================================
Thank you for your patience and cooperation. If you have any questions orconcerns, please feel free to post it in the group. I am standing by to beof assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no

rights.

Nov 15 '05 #9

P: n/a

Hi localhost,

Thanks very much for your feedback.

I am glad my reply makes sense to you.

Yes, use try...catch in the code is a good habit which enable robust code
in your application.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #10

P: n/a
Hi localhost,

Do you still have any concern?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.