GetChildRows() Fails.

M

MioTheGreat

Ok. So here's my situation. I've created a small database in SQL Server
2005 that looks like this:

CREATE TABLE [dbo].[TblB](
[Id] [int] NOT NULL,
[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[TblB](
[Id] [int] NOT NULL,
[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

ALTER TABLE [dbo].[TblB] WITH CHECK ADD CONSTRAINT [FK_TblB_TblA]
FOREIGN KEY([Id])
REFERENCES [dbo].[TblA] ([Id])

Simple enough. Two tables with a simple relation between them.

I drag and drop the tables from the server explorer into a blank
Dataset in Visual Studio 2005....everything seems to work, life is
good.

However. On a TblARow, I have a method called GetTblBRows(). This is
understandable. There is a relation between TblA and TblB. If I try to
invoke it, I get an exception. InvalidCastException, to be precise.

Here's my code (Minus the dataset. The dataset is long, boring, and is
just what you get with a drag and drop from Visual Studio with no
modifications by me.)

class Program
{
static void Main(string[] args)
{
DataSet1TableAdapters.TblATableAdapter ta = new
ConsoleApplication2.DataSet1TableAdapters.TblATableAdapter();
DataSet1.TblADataTable dt = ta.GetData();
DataSet1.TblARow row = (DataSet1.TblARow)dt.Rows[0];

//This line throws an InvalidCastException.
DataSet1.TblBRow[] tb2 = row.GetTblBRows();

foreach (DataSet1.TblBRow r in tb2)
{
Console.WriteLine(r.Name);
}
Console.ReadLine();
}
}

In Dataset1.Designer.cs, GetTblBRows() is defined as:

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public TblBRow[] GetTblBRows() {
return
((TblBRow[])(base.GetChildRows(this.Table.ChildRelations["FK_TblB_TblA"])));
}

Rather obviously, this line is throwing the exception (If you remove
the DebuggerNonUserCode attribute, it appears there.)

It says it cannot convert from TblARow[] to TblBRow[]. But that doesnt'
make any sense. If I have a TblARow[], and a relation to another table,
shouldn't the GetChildRows() with that relation return that other
table's data?
 
C

Cowboy \(Gregory A. Beamer\)

GetChildRows should pull rows from the child table. There are a couple of
possibilities.

1. No relationship set up in the XSD
2. Syntax incorrect

The syntax is pulling from base, which could be an issue. The order of load
might get the tables out of sync, as well (I have found that the order of
the tables in XSD sometimes matters).

Ultimately, however, you are pulling table A rows in this function, not
table B rows and attempting to cast them out as table B rows.

One thing to try is breaking out your mega call into separate lines and
seeing what you actually have. This will help you determine the root cause
of your issue.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
Ok. So here's my situation. I've created a small database in SQL Server
2005 that looks like this:

CREATE TABLE [dbo].[TblB](
[Id] [int] NOT NULL,
[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[TblB](
[Id] [int] NOT NULL,
[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

ALTER TABLE [dbo].[TblB] WITH CHECK ADD CONSTRAINT [FK_TblB_TblA]
FOREIGN KEY([Id])
REFERENCES [dbo].[TblA] ([Id])

Simple enough. Two tables with a simple relation between them.

I drag and drop the tables from the server explorer into a blank
Dataset in Visual Studio 2005....everything seems to work, life is
good.

However. On a TblARow, I have a method called GetTblBRows(). This is
understandable. There is a relation between TblA and TblB. If I try to
invoke it, I get an exception. InvalidCastException, to be precise.

Here's my code (Minus the dataset. The dataset is long, boring, and is
just what you get with a drag and drop from Visual Studio with no
modifications by me.)

class Program
{
static void Main(string[] args)
{
DataSet1TableAdapters.TblATableAdapter ta = new
ConsoleApplication2.DataSet1TableAdapters.TblATableAdapter();
DataSet1.TblADataTable dt = ta.GetData();
DataSet1.TblARow row = (DataSet1.TblARow)dt.Rows[0];

//This line throws an InvalidCastException.
DataSet1.TblBRow[] tb2 = row.GetTblBRows();

foreach (DataSet1.TblBRow r in tb2)
{
Console.WriteLine(r.Name);
}
Console.ReadLine();
}
}

In Dataset1.Designer.cs, GetTblBRows() is defined as:

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public TblBRow[] GetTblBRows() {
return
((TblBRow[])(base.GetChildRows(this.Table.ChildRelations["FK_TblB_TblA"])));
}

Rather obviously, this line is throwing the exception (If you remove
the DebuggerNonUserCode attribute, it appears there.)

It says it cannot convert from TblARow[] to TblBRow[]. But that doesnt'
make any sense. If I have a TblARow[], and a relation to another table,
shouldn't the GetChildRows() with that relation return that other
table's data?
 
B

Bishman

I found this issue the first time I used GetChildRows as well. I solved it
FIRST time in the same way. fill the table (Adapter).

However I also came accross it again, and the TableAdapter ( In my case )
was filled this time.

I found no resolution......

Not much help to anyone but some other ideas may come through.....!

JB
 

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