Differnt behavior of ADO.Net Relation.Nested under 1.1

  • Thread starter Thread starter Butch
  • Start date Start date
B

Butch

I have an example from MSDN that seems to have worked
under 1.0. It does not under 1.1.
This is the survey example found at:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnaspp/html/surveysample2.asp

The write up found there explains how and why the author
must change from code some of the properties of her
strongly typed dataset. Her code apparently runs fine on
1.0 but will error at rel.Nested = true when using Net
Framework V1.1.

Here is her code:

Public Class ResponseDataSet
Inherits BaseResponseDataSet

Public Sub New()
MyBase.New()
AdjustQuestionPrimaryKey()
End Sub

Protected Sub New(ByVal info As SerializationInfo,_
ByVal context As StreamingContext)
MyBase.New(info, context)
AdjustQuestionPrimaryKey()
End Sub

' This method sets the primary key for the Question
table
' to the Id column drawn from the original data
' rather than the Id column created by the
autogenerated code
Public Sub AdjustQuestionPrimaryKey()

Answer.Constraints.Remove("Question_Answer")
Question.Constraints.Clear()

Question.PrimaryKey = New DataColumn()
{Question.Columns("Id")}

Dim fkc As ForeignKeyConstraint
fkc = New ForeignKeyConstraint("Question_Answer",_
New DataColumn() {Question.Columns
("Id")},_
New DataColumn(){Answer.Columns
("Question_Id")})
fkc.AcceptRejectRule = AcceptRejectRule.None
fkc.DeleteRule = Rule.Cascade
fkc.UpdateRule = Rule.Cascade
Answer.Constraints.Add(fkc)

Relations.Remove("Question_Answer")
Dim rel As DataRelation = New DataRelation
("Question_Answer",_
New DataColumn() {Question.Columns
("Id")},_
New DataColumn() {Answer.Columns
("Question_Id")},_
False)
rel.Nested = True
Relations.Add(rel)
End Sub
End Class

Here is the error thrown when hitting the rel.Nested =
True line:

A column named 'Answer' already belongs to this
DataTable: cannot set a nested table name to the same
name.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.Data.DuplicateNameException: A
column named 'Answer' already belongs to this DataTable:
cannot set a nested table name to the same name.
 
There may be something wrong with the XML, The error message is pretty specific that a tnested table is trying to be named the same as a column name and that this cannot be
done... The following code does not reproduce any errors and outputs a properly nested XML output...

Dim nwindConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection("Data Source=MyServer;uid=sa;pwd=Password;Initial
Catalog=Northwind;")
Dim custDA As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers",
nwindConn)
Dim orderDA As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter("SELECT OrderID, CustomerID, OrderDate FROM Orders",
nwindConn)

nwindConn.Open()

Dim custDS As DataSet = New DataSet("CustomerOrders")
custDA.Fill(custDS, "Customers")
orderDA.Fill(custDS, "Orders")

nwindConn.Close()

Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", custDS.Tables("Customers").Columns("CustomerID"), custDS.Tables("Orders").Columns
("CustomerID"))
custOrderRel.Nested = True
custDS.WriteXml("D:\MyTest.xml")

so it appears that Relation.Nested is working properly in 1.1...

Want to know more? Check out the MSDN Library at http://msdn.microsoft.com or the Microsoft Knowledge Base at http://support.microsoft.com

Scot Rose, MCSD
Microsoft Visual Basic Developer Support
Email : (e-mail address removed) <Remove word online. from address>

This posting is provided “AS IS”, with no warranties, and confers no rights.




--------------------
 
OK, something in xml then. I am stuck. Why did code
execute as expected under vs net 2002 with framework 1.0.
and now not behave correctly ? The only change here is
new framework.
I'd like to make the sample work. Much of the
functionality is usefull to me. I have seen some others
with this problem on other news groups.
Do you know if authors of msdn article are aware that
code is broken ?
The code is broken ? You reproduced error ?
Anyway thanks for help so far.
-----Original Message-----
There may be something wrong with the XML, The error
message is pretty specific that a tnested table is trying
to be named the same as a column name and that this
cannot be
done... The following code does not reproduce any errors
and outputs a properly nested XML output...
Dim nwindConn As
System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection("Data
Source=MyServer;uid=sa;pwd=Password;Initial
Catalog=Northwind;")
Dim custDA As
System.Data.SqlClient.SqlDataAdapter = New
System.Data.SqlClient.SqlDataAdapter("SELECT CustomerID,
CompanyName FROM Customers",
nwindConn)
Dim orderDA As
System.Data.SqlClient.SqlDataAdapter = New
System.Data.SqlClient.SqlDataAdapter("SELECT OrderID,
CustomerID, OrderDate FROM Orders",
nwindConn)

nwindConn.Open()

Dim custDS As DataSet = New DataSet ("CustomerOrders")
custDA.Fill(custDS, "Customers")
orderDA.Fill(custDS, "Orders")

nwindConn.Close()

Dim custOrderRel As DataRelation =
custDS.Relations.Add("CustOrders", custDS.Tables
("Customers").Columns("CustomerID"), custDS.Tables
("Orders").Columns
("CustomerID"))
custOrderRel.Nested = True
custDS.WriteXml("D:\MyTest.xml")

so it appears that Relation.Nested is working properly in 1.1...

Want to know more? Check out the MSDN Library at
http://msdn.microsoft.com or the Microsoft Knowledge
Base at http://support.microsoft.com
 
Check the XML and find out if there is in fact a Column name with the same name as a nested table, If there is, modify one or the other and see if the error still occurs...

Want to know more? Check out the MSDN Library at http://msdn.microsoft.com or the Microsoft Knowledge Base at http://support.microsoft.com

Scot Rose, MCSD
Microsoft Visual Basic Developer Support
Email : (e-mail address removed) <Remove word online. from address>

This posting is provided “AS IS”, with no warranties, and confers no rights.




--------------------
 
Back
Top