473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Custom Event for DataSet Read?


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
10 1502

"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

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

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

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

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
/*

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
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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: T | last post by:
I have a web site i want to deploy using a VS.NEt set up project. I have no problems with that, that is fine and works no problem. But the web application uses a custom event log to log...
1
by: Victor | last post by:
I am developing a windows service application that uses a custom event. the problem is that the service installer associates the service with application log during the installation process....
3
by: bruce | last post by:
Hello, I am trying to figure out how to use a class with a custom event as a wrapper for a subform so that I can automatically trigger things to happen on the parent form when the custom event...
5
by: Simon Middlemiss | last post by:
I am writting a program to manage DTS packages which is based on the code example at the following link http:\\www.support.microsoft.com/?kbid=319985. I need to do things in a Windows Forms...
3
by: bill | last post by:
I am using VS2005 to build a web form dynamically. I'm using AddHandler to connect a custom event handler to the TextChanged event of dynamically added textbox controls. Data entered in the...
2
by: T McDonald | last post by:
I've been having a problem writing to an event to the Application log. I believe the issue is caused by the fact that the source was previously both a custom log and the source. I deleted the...
2
by: trialproduct2004 | last post by:
Hi all, I am having application in whihc i am inserting menuitem dynamically. When i add menuitem to mainmenu, i want to pass extra parameter to eventargs. So what i did is derived class from...
6
by: tshad | last post by:
I was looking at a page that showed how to set up a custom event and it seems to work ok. But I am not sure how I would use it. How would I subscribe to it. There is actual action (such as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.