473,386 Members | 1,745 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,386 software developers and data experts.

I'm completely stuck.............

I post a message about this, but noone responded.. Well, I've played
around as much as I can and I'm completely stuck and out of ideas.

This should be a really simple task, but it seems that it is beyond me
why this has to be so hard to do.

I have two tables, the ID is an Autonumber in Table 1. Table two is the
child to the previous table. Table 2 is in a grid. I enter information
in table 1, then I go to the grid and enter a row there. Then I do to
save, which does the Update, and it fails on table one saying that an
object hasn't been instantied, but it still saves the record, but also
creates another record, with a new ID from the SQL server. The previous
record still has 0 in the ID, and has the table 2 record related to it.
The new record in table 1 has no child records. If I just simply hit
save again, it comes up with this Foreign Key constraint error that says
that parent records must exist for this child record.

I've tried removing the ID in the insert and update statments for the
child, I've tried saving the parent before entering the grid. (I'm doing
an EndCurrentEdit right now) Nothing seems to fix it. A lot of changes
make it worse.

How the heck are you supposed to do this? It seems like a simple task,
but it's becoming a severe head ache.
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.
Nov 21 '05 #1
4 1149
My advice:

don't use Identity columns as primary keys. Use GUIDs.
Your app can generate the primary key value without any help from SQL Server
(no need for a round trip).

So, when a user wants to add a parent row, create the guid and associate it
with the new data.
When the user wants to add a child row, use the Guid as the foreign key.

Save to the database. SQL doesn't autogenerate the Guids, so there's no
conflict, and no need for a round trip.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Aaron Smith" <th**********@smithcentral.net> wrote in message
news:5q*****************@newssvr31.news.prodigy.co m...
I post a message about this, but noone responded.. Well, I've played
around as much as I can and I'm completely stuck and out of ideas.

This should be a really simple task, but it seems that it is beyond me
why this has to be so hard to do.

I have two tables, the ID is an Autonumber in Table 1. Table two is the
child to the previous table. Table 2 is in a grid. I enter information
in table 1, then I go to the grid and enter a row there. Then I do to
save, which does the Update, and it fails on table one saying that an
object hasn't been instantied, but it still saves the record, but also
creates another record, with a new ID from the SQL server. The previous
record still has 0 in the ID, and has the table 2 record related to it.
The new record in table 1 has no child records. If I just simply hit
save again, it comes up with this Foreign Key constraint error that says
that parent records must exist for this child record.

I've tried removing the ID in the insert and update statments for the
child, I've tried saving the parent before entering the grid. (I'm doing
an EndCurrentEdit right now) Nothing seems to fix it. A lot of changes
make it worse.

How the heck are you supposed to do this? It seems like a simple task,
but it's becoming a severe head ache.
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #2
Nick Malik [Microsoft] wrote:
My advice:

don't use Identity columns as primary keys. Use GUIDs.
Your app can generate the primary key value without any help from SQL Server
(no need for a round trip).

So, when a user wants to add a parent row, create the guid and associate it
with the new data.
When the user wants to add a child row, use the Guid as the foreign key.

Save to the database. SQL doesn't autogenerate the Guids, so there's no
conflict, and no need for a round trip.


Took your advice. Started to use Guids. The error messages have gone
away. One problem... No child records are being saved to the SQL Server
now. Even if I create a parent, save, close the program and open it,
then add child records. No errors. No children in the server. It does an
update, the DataSet.HasChanges() returns false. Look at the table on the
server, no children.

--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.
Nov 21 '05 #3
Nick Malik [Microsoft] wrote:
fascinating.

Code would help. It's probably a minor oversight somewhere.


It's fixed.

After messing with it all day (for the last 4 days) I have finally
solved the issue. Now, keep in mind I haven't been using VB.Net for very
long.. With that said. Here is my original update procedure:

Public Function UpdateData() As Boolean
Dim bErr As Boolean = True
Try
Me.CourseDA.Update(CoursesDS1, "Courses")
Me.InstClassDA.Update(CoursesDS1, "InstClasses")
Catch ex As Exception
Misc.ShowError(ex.Message())
bErr = False
End Try
Return bErr
End Function

Courses is parent, InstClasses is child.

Bad idea, I guess. When the update is done for Courses, all the
changed/added/deleted rows in InstClasses have their RowState set to
Unchanged. I guess when update is done, it sets all the rowstates to
Unchanged for every table in the DataSet.

I now do this:

Public Function UpdateData() As Boolean
Dim bErr As Boolean = True
Try
Dim cdt As DataTable = CoursesDS1.Courses.GetChanges()
Dim cldt As DataTable = CoursesDS1.InstClasses.GetChanges()
If Not cdt Is Nothing Then
Me.CourseDA.Update(cdt)
End If
If Not cldt Is Nothing Then
Me.InstClassDA.Update(cldt)
End If
CoursesDS1.Merge(cdt)
CoursesDS1.Merge(cldt)
CoursesDS1.AcceptChanges()
Catch ex As Exception
Misc.ShowError(ex.Message())
bErr = False
End Try
Return bErr
End Function

And it saves everything just fine. Now I just have to figure out how to
do the deletes because you have to delete the child first... Then I
should be ok.

--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.
Nov 21 '05 #4
clever. I'm glad you figured it out. I just got back online. Sorry I
couldn't help sooner.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Aaron Smith" <th**********@smithcentral.net> wrote in message
news:In*****************@newssvr31.news.prodigy.co m...
Nick Malik [Microsoft] wrote:
fascinating.

Code would help. It's probably a minor oversight somewhere.


It's fixed.

After messing with it all day (for the last 4 days) I have finally
solved the issue. Now, keep in mind I haven't been using VB.Net for very
long.. With that said. Here is my original update procedure:

Public Function UpdateData() As Boolean
Dim bErr As Boolean = True
Try
Me.CourseDA.Update(CoursesDS1, "Courses")
Me.InstClassDA.Update(CoursesDS1, "InstClasses")
Catch ex As Exception
Misc.ShowError(ex.Message())
bErr = False
End Try
Return bErr
End Function

Courses is parent, InstClasses is child.

Bad idea, I guess. When the update is done for Courses, all the
changed/added/deleted rows in InstClasses have their RowState set to
Unchanged. I guess when update is done, it sets all the rowstates to
Unchanged for every table in the DataSet.

I now do this:

Public Function UpdateData() As Boolean
Dim bErr As Boolean = True
Try
Dim cdt As DataTable = CoursesDS1.Courses.GetChanges()
Dim cldt As DataTable = CoursesDS1.InstClasses.GetChanges()
If Not cdt Is Nothing Then
Me.CourseDA.Update(cdt)
End If
If Not cldt Is Nothing Then
Me.InstClassDA.Update(cldt)
End If
CoursesDS1.Merge(cdt)
CoursesDS1.Merge(cldt)
CoursesDS1.AcceptChanges()
Catch ex As Exception
Misc.ShowError(ex.Message())
bErr = False
End Try
Return bErr
End Function

And it saves everything just fine. Now I just have to figure out how to
do the deletes because you have to delete the child first... Then I
should be ok.

--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: No | last post by:
I purchased a program ( So I thought) that turned out to be just a bunch of php scripts put together. I am getting terrible support from the author but I am biting my tongue because I don't want...
8
by: MLH | last post by:
I use a mouse-down procedure to trap right mouse clicks and CTRL-Right mouse clicks. Running the procedure must put honey or some other sticky substance into my keyboard because subsequent...
4
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
2
by: Eugene | last post by:
Hi all, Probably an easy question, but I'm kinda stuck. Here's the scenario - I have a datagrid with, let's say, 10 rows. Now, if I click on a cell in 10th row, and then update underlying...
4
by: =?Utf-8?B?TWF1cg==?= | last post by:
My cd is stuck in the drive. I can open the drawer O K but the c d will not come out Help me please -- Maur
0
by: =?Utf-8?B?QmlsbEI=?= | last post by:
This is a tough one... My Windows Service app periodically performs some processing on new entries to a SQL Server database table. It uses a simple System.Timer to call the Elapsed event...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
7
by: alphasahoo | last post by:
Hi I am working on a program which writes the output a SQL select statements from number of source tables first to a load matrix and then writes to a load.dat file. But while writing to the...
17
by: Bill Cunningham | last post by:
I have this code and it will not compile telling me that pow is undefined. I'm not quite sure what to make of this so I thought I'd get some feedback. #include <stdio.h> #include <stdlib.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.