Parent/child table problems

B

Bob Day

Using VS 2003, VB.NET, SQL MSDE..

I am stumped. I have a datagrid with a parent/child relationship between
two tables. If I add 200 rows from code in the same win form to the child
table with only the parent table displayed, it works fine, and then you can
display the child table and all data is as expectect.

If I display the child table, and do the exact same thing, try to add 200
rows, only a a few (max 10) are added and displayed, and then I get error
messages like below at the "form.showdialog" that displayed the win form to
begin with:

This is the 1st error after about 10 rows added
System.NullReferenceException - object refereced not set to an instance of
an object



If you hit continue, you get this error next

System.runtime.interopservices.SEHException - external component has thrown
an exception



These are using DataAdapters created in component designer, with max
connections set to 800 in my SQL string to avoid know bug of connections not
being released.



What is up?



Bob Day
 
T

Tian Min Huang

Hello Bob,

Thanks for your post. I reviewed your description carefully, and think more
information is needed before moving forward:

1. Please check the call stack and tell me when it throws the exception.
2. Is it possible for you to post a simple project which is able to
reproduce the problem? I will be glad to check it on my side.

I look forward to hearing from you.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bob Day

Can you send me or point me to a URL for an example of an partent/child
table that 200 rows are simultaneously added via 2 thread to the child table
with the child table displayed that works?

I have spend days on this, and cannot get it to work. At this point, I am
considering dumping it and going a different route.

I cannot see how to send you a simply example, since it is all coming from
SQL.

Thanks
Bob Day
 
T

Tian Min Huang

Hello Bob,

Thanks for your response. Now I understand that your application is
multi-threaded that you add rows to child table from within another thread.
Please correct me if there is any misunderstanding. I'd like to share the
following information with you:

I believe that the exception NullReferenceException may be caused by
synchronization issue in your application. In a multi-threaded application,
you have to do some synchronization on data to prevent two threads access
one data at the same time. To work around the problem, you should add rows
to child table in the main thread. Please refer to the following code
snippet:

//----------------code snippet-----------------------
private System.Data.DataRow dr;

public DG()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

dataGrid.DataSource = myDataset;
dr = null;

ThreadStart threadStart = new ThreadStart(listenThread);
Thread thread = new Thread(threadStart);
thread.IsBackground = true;
thread.Start();
}

private void listenThread() //Generate new data
{
while ( running )
{
lock(this)
{
if(this.dr == null)
{
dr = myDataset.MyTable.NewMyTableRow();
dr[0] = DateTime.Now;
dr[1] = random.Next();
}
}

this.Invoke(new MethodInvoker(AddData)); //Post a message to the main
thread.
Thread.Sleep(200);
}
}

private void AddData() //Receive message from another thread and add row
to the dataset
{
//This one works in the main thread.
lock(this)
{
if(this.dr != null)
this.myDataset.MyTable.Rows.Add(dr);
this.dr = null;
}
}
//-------------------------end of--------------------------------

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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