Mixing Strongly Type and Regular DataSets

G

Guest

My apps are written without using strongly typed dataSets. I am in the process of converting them over but don't want to
rewrite the whole app with STDS at this time. So I am trying to put them into new code and eventually I will get them throughout the app, or at least that is my theory :)

So I thought I would start by trying to use a STDS from the exsisting dataSets whereever I can but am running into an issue with the primary key. When I create a new Table from the STDS using a table from the dataSet it doesn't copy in the primary key info. See Example below.

So why doesnt the strongly typed DataTable take the primary keys from the table passed to it in its constructor???

public static bool CreateClass(string fullClassName, DataSet dataSet)
{
result = String.Empty;

//Try to Load STDS table from Existing Untyped Table in UnTyped DataSet

RadData.ClassesDataTable classesTable
= new RadData.ClassesDataTable( dataSet.Tables["Classes"] );

//If you check classes table at this point no Primary keys columns were created???
//even though the table passed to it has primary keys (SEE the STDS autoCreated Code Below)

//Also Create a Regular untyped Table
DataTable classesUnTypedTable
= dataSet.Tables["Classes"] ;

//If you checkclassesUnTypedTable at this point it has Primary keys columns as expected

//
int classNameStart = fullClassName.LastIndexOf(".") + 1;

if (classNameStart >= fullClassName.Length - 1
||classNameStart <= 0 )
{
result = "Class Name invalid";
return false;
}

string className = fullClassName.Substring(classNameStart);
string nameSpace = fullClassName.Substring(0,classNameStart - 1);

Object[] keys = new Object[]{className,nameSpace};

//Finds the rows
DataRow rowUntyped
= classesUnTypedTable.Rows.Find(keys);

//Bombs as table has no primary key
RadData.ClassesRow row
= classesTable.FindByClassNameNameSpace(className,nameSpace);

*********************************************************
Here is the AutoMagically generatred code for the STDS from VS 2003
*********************************************************
internal ClassesDataTable(DataTable table) :
base(table.TableName) {
if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
this.CaseSensitive = table.CaseSensitive;
}
if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
this.Locale = table.Locale;
}
if ((table.Namespace != table.DataSet.Namespace)) {
this.Namespace = table.Namespace;
}
this.Prefix = table.Prefix;
this.MinimumCapacity = table.MinimumCapacity;
this.DisplayExpression = table.DisplayExpression;
}
 
M

Miha Markic [MVP C#]

Hi Mike,

I am not sure if I understand your problem correctly.
If you don't set the table primary key in dataset designer the strong typed
dataset won't have additional methods/properties regarding primary key.
However the underlying table will still have the primary key.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Mike in Paradise said:
My apps are written without using strongly typed dataSets. I am in the
process of converting them over but don't want to
rewrite the whole app with STDS at this time. So I am trying to put them
into new code and eventually I will get them throughout the app, or at least
that is my theory :)
So I thought I would start by trying to use a STDS from the exsisting
dataSets whereever I can but am running into an issue with the primary key.
When I create a new Table from the STDS using a table from the dataSet it
doesn't copy in the primary key info. See Example below.
So why doesnt the strongly typed DataTable take the primary keys from the
table passed to it in its constructor???
public static bool CreateClass(string fullClassName, DataSet dataSet)
{
result = String.Empty;

//Try to Load STDS table from Existing Untyped Table in UnTyped DataSet

RadData.ClassesDataTable classesTable
= new RadData.ClassesDataTable( dataSet.Tables["Classes"] );

//If you check classes table at this point no Primary keys columns were created???
//even though the table passed to it has primary keys (SEE the STDS autoCreated Code Below)

//Also Create a Regular untyped Table
DataTable classesUnTypedTable
= dataSet.Tables["Classes"] ;

//If you checkclassesUnTypedTable at this point it has Primary keys columns as expected

//
int classNameStart = fullClassName.LastIndexOf(".") + 1;

if (classNameStart >= fullClassName.Length - 1
||classNameStart <= 0 )
{
result = "Class Name invalid";
return false;
}

string className = fullClassName.Substring(classNameStart);
string nameSpace = fullClassName.Substring(0,classNameStart - 1);

Object[] keys = new Object[]{className,nameSpace};

//Finds the rows
DataRow rowUntyped
= classesUnTypedTable.Rows.Find(keys);

//Bombs as table has no primary key
RadData.ClassesRow row
= classesTable.FindByClassNameNameSpace(className,nameSpace);

*********************************************************
Here is the AutoMagically generatred code for the STDS from VS 2003
*********************************************************
internal ClassesDataTable(DataTable table) :
base(table.TableName) {
if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
this.CaseSensitive = table.CaseSensitive;
}
if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
this.Locale = table.Locale;
}
if ((table.Namespace != table.DataSet.Namespace)) {
this.Namespace = table.Namespace;
}
this.Prefix = table.Prefix;
this.MinimumCapacity = table.MinimumCapacity;
this.DisplayExpression = table.DisplayExpression;
}
 
G

Guest

The structure is the same as both are created from the exact same .xsd file. The unType dataSet is instanciated with a ReadXml using the xsd as a Schema. This is the Same Scehma definition that the Generate strongly typed dataSet in VS is using.

The issue is:is there a way of making a strongly typed dataSet or Table reference an unTyped dataSet/table instance.

In the application there is a dataHandler that returns a unTyped dataSet. All the apps are currently using this.
What I want to do is start to used Strongly typed DataSets with new apps using the unTyped DataSet being returned from
the DataManager but loading it into a strongly type dataSet for the new applications.

Eventually (assuming performance is the same) is change everthing over to STDS..
 
G

Guest

To clarify this further I guess what I am trying to do (may not be possible) is Get a strongtyped DataSet/Table to reference an instance of an untyped dataSet/Table

It looks like I might have to just use the Merge Method to get data moved back and forth between typed and untyped..
 
M

Miha Markic [MVP C#]

Hi Mike,

I see.
Yes, unfortuantelly this is the easiest and probably the only solution.
The other would be to derive a dataset from your strong typed dataset class
and do the same as strong typed dataset constructor does.
Minus creating tables.
This would involve reflection since there are some private methods.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Mike in Paradise said:
To clarify this further I guess what I am trying to do (may not be
possible) is Get a strongtyped DataSet/Table to reference an instance of an
untyped dataSet/Table.
It looks like I might have to just use the Merge Method to get data moved
back and forth between typed and untyped..
 

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