Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Nested Repeaters


Message #1 by Joe <mindwalker@i...> on Sat, 22 Feb 2003 02:47:10 -0600
Suppose I have the following ASP code, which displays a master-detail
view of two tables:
Set rs1 = db.Execute("SELECT * FROM Orders WHERE X=1 AND
Y=2")
' x=1 and y=2 represent any arbitrary filter criteria, just as an
example
' but the important point is that I'm not retrieving *all* orders
only
' a subset
Do Until rs1.EOF
  Response.Write "<u>" &
rs1("OrderID") & "</u><br>"
  Set rs2 = db.Execute("SELECT * FROM OrderItems WHERE
OrderID=" & rs1("OrderID")
  Do Until rs2.EOF
    Response.Write rs2("Item") &
"<br>"
    rs2.MoveNext
  Loop
  Set rs2 = Nothing
  Response.Write "<br>"
  rs1.MoveNext
Loop
Set rs1 = Nothing
how would I do it in ASP.NET (C# preferably, but VB okay too) using
nested DataRepeaters? I have found a few articles that deal with this
issue, such as 
http://www.123aspx.com/redir.aspx?res=29338
and
http://www.123aspx.com/redir.aspx?res=29259
but they both rely on creating a DataRelation between two tables Orders
and OrderItems, which only seems to work when you retrieve *all* records
from Orders, not just a subset, because otherwise it will result in an
error "This constraint cannot be enabled as not all values have
corresponding parent values." I know there must be a simple way
to get around this, but I can't figure it out. Here is a snippet of my
code:
        DataSet
myDataSet = new DataSet();
        SqlDataAdapter
myAdapter = new
SqlDataAdapter();        
        
        myAdapter.SelectCommand
= myCommand; // myCommand gets a subset of the data in
        myAdapter.Fill(myDataSet,
"Orders"); // the orders table matching certain criteria
        
        myAdapter.SelectCommand
= new SqlCommand("GetOrderItems", myConnection);
        myAdapter.Fill(myDataSet,
"OrderItems");
        
        //myDataSet.Tables["OrderItems
        
  // We need to define a relationship between the two tables in the
dataset
        DataRelation
myRelation = new DataRelation("OrderID",
myDataSet.Tables["Orders"].Columns["OrderID"], 
myDataSet.Tables["OrderItems"].Columns["OrderID"],
true);
        myDataSet.Relations.Add(myRelation);
        
this last line is what causes the error mentioned above. How do I avoid
this error and still achieve the results I'm looking for?



  Return to Index