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?
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?