Datarelation between different column types

J

Jason James

Hi,

I am trying to create a new datarelation between columns
in two tables. The data types of the columns are Integer
and Double, and when I try and create the data relation
I get an invalid constraint exception thrown that says:

Parent columns and child columns don't have type- matching columns'

Is there anyway of getting around this issue, other than
changing the types of the columns? I am linking to the
data using an MS Access database (the data is an ODBC source)
and so I can't change the type!

Even though the type of the column that MS Access has
linked to is Double, it does in fact only contain integer
values.

Many thanks,

Jason.
 
M

Miha Markic [MVP C#]

Hi Jason,

Did you try changing just DataColumn's type from double to integer?
 
J

Jason James

Can I do this in VB.Net? I can't change the type in the linked
field as Access won't let me!

I'll give it a go and let you know.

Thanks,

Jason

Hi Jason,

Did you try changing just DataColumn's type from double to integer?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Jason James said:
Hi,

I am trying to create a new datarelation between columns
in two tables. The data types of the columns are Integer
and Double, and when I try and create the data relation
I get an invalid constraint exception thrown that says:

Parent columns and child columns don't have type- matching columns'

Is there anyway of getting around this issue, other than
changing the types of the columns? I am linking to the
data using an MS Access database (the data is an ODBC source)
and so I can't change the type!

Even though the type of the column that MS Access has
linked to is Double, it does in fact only contain integer
values.

Many thanks,

Jason.
 
J

Jason James

Miha,

I got there in the end. I had to load the schema of the data into the
dataset first, as you can't modify a datatype once the field contains
data. Once I'd loaded the schema and then modified the field,
and then loaded the actual data I was able to add the data relation.

Many thanks,

Jason.

Hi Jason,

Did you try changing just DataColumn's type from double to integer?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Jason James said:
Hi,

I am trying to create a new datarelation between columns
in two tables. The data types of the columns are Integer
and Double, and when I try and create the data relation
I get an invalid constraint exception thrown that says:

Parent columns and child columns don't have type- matching columns'

Is there anyway of getting around this issue, other than
changing the types of the columns? I am linking to the
data using an MS Access database (the data is an ODBC source)
and so I can't change the type!

Even though the type of the column that MS Access has
linked to is Double, it does in fact only contain integer
values.

Many thanks,

Jason.
 
A

AMDRIT

If you didn't want to use a strongly typed dataset:

Sample #1 (you could have specified the data format you wanted for each
column.)

dataapater1.selectcommand.commandtext = "Select cdbl(column1) as column1,
column2, column3 from parenttable"
dataapater2.selectcommand.commandtext = "Select column1, column2,
cdbl(column3) as column3 from childtable"

ds.Relations.Add(New DataRelation("ParentChild", dt1.Columns(0),
dt2.Columns("Column3"), True))

Sample #2 (you could have defined the table first, then load it all via
code.)

Using the strongly typed dataset generator, a lot of work is done for you
with a couple clicks of the mouse.
Here is one manual way of doing it.

Dim ds As DataSet
Dim dt1 As DataTable, dt2 As DataTable

'Instantiate the dataset
ds = New DataSet("Warehouse")

'Instantiate teh datatables
dt1 = New DataTable("MyParentTable")
dt2 = New DataTable("MyChildTable")

'Add the tables to the set
ds.Tables.Add(dt1)
ds.Tables.Add(dt2)

'Define the first table's columns
dt1.Columns.Add(New DataColumn("column1", GetType(System.Double)))
dt1.Columns.Add(New DataColumn("column2", GetType(System.String)))

'Create a Primary Key

dt1.PrimaryKey = New DataColumn() {dt1.Columns(0)}

'Define the second table's columns
dt2.Columns.Add(New DataColumn("column1", GetType(System.Int32)))
dt2.Columns.Add(New DataColumn("column2", GetType(System.String)))
dt2.Columns.Add(New DataColumn("column3", GetType(System.Double)))

'Create a Primary Key
dt1.PrimaryKey = New DataColumn() {dt1.Columns("Column1")}

'Retrieve the data
mydataadapter1.fill(dt1)
mydataadapter2.fill(dt2)

'Create Relationship
ds.Relations.Add(New DataRelation("ParentChild", dt1.Columns(0),
dt2.Columns("Column3"), True))


Jason James said:
Miha,

I got there in the end. I had to load the schema of the data into the
dataset first, as you can't modify a datatype once the field contains
data. Once I'd loaded the schema and then modified the field,
and then loaded the actual data I was able to add the data relation.

Many thanks,

Jason.

Hi Jason,

Did you try changing just DataColumn's type from double to integer?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Jason James said:
Hi,

I am trying to create a new datarelation between columns
in two tables. The data types of the columns are Integer
and Double, and when I try and create the data relation
I get an invalid constraint exception thrown that says:

Parent columns and child columns don't have type- matching columns'

Is there anyway of getting around this issue, other than
changing the types of the columns? I am linking to the
data using an MS Access database (the data is an ODBC source)
and so I can't change the type!

Even though the type of the column that MS Access has
linked to is Double, it does in fact only contain integer
values.

Many thanks,

Jason.
 
J

Jason James

I like the first suggestion, and casting the field to a different type
before you execute the SELECT query is a nice way of solving
the problem.

Many thanks,

Jason.

If you didn't want to use a strongly typed dataset:

Sample #1 (you could have specified the data format you wanted for each
column.)

dataapater1.selectcommand.commandtext = "Select cdbl(column1) as column1,
column2, column3 from parenttable"
dataapater2.selectcommand.commandtext = "Select column1, column2,
cdbl(column3) as column3 from childtable"

ds.Relations.Add(New DataRelation("ParentChild", dt1.Columns(0),
dt2.Columns("Column3"), True))

Sample #2 (you could have defined the table first, then load it all via
code.)

Using the strongly typed dataset generator, a lot of work is done for you
with a couple clicks of the mouse.
Here is one manual way of doing it.

Dim ds As DataSet
Dim dt1 As DataTable, dt2 As DataTable

'Instantiate the dataset
ds = New DataSet("Warehouse")

'Instantiate teh datatables
dt1 = New DataTable("MyParentTable")
dt2 = New DataTable("MyChildTable")

'Add the tables to the set
ds.Tables.Add(dt1)
ds.Tables.Add(dt2)

'Define the first table's columns
dt1.Columns.Add(New DataColumn("column1", GetType(System.Double)))
dt1.Columns.Add(New DataColumn("column2", GetType(System.String)))

'Create a Primary Key

dt1.PrimaryKey = New DataColumn() {dt1.Columns(0)}

'Define the second table's columns
dt2.Columns.Add(New DataColumn("column1", GetType(System.Int32)))
dt2.Columns.Add(New DataColumn("column2", GetType(System.String)))
dt2.Columns.Add(New DataColumn("column3", GetType(System.Double)))

'Create a Primary Key
dt1.PrimaryKey = New DataColumn() {dt1.Columns("Column1")}

'Retrieve the data
mydataadapter1.fill(dt1)
mydataadapter2.fill(dt2)

'Create Relationship
ds.Relations.Add(New DataRelation("ParentChild", dt1.Columns(0),
dt2.Columns("Column3"), True))


Jason James said:
Miha,

I got there in the end. I had to load the schema of the data into the
dataset first, as you can't modify a datatype once the field contains
data. Once I'd loaded the schema and then modified the field,
and then loaded the actual data I was able to add the data relation.

Many thanks,

Jason.

Hi Jason,

Did you try changing just DataColumn's type from double to integer?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Hi,

I am trying to create a new datarelation between columns
in two tables. The data types of the columns are Integer
and Double, and when I try and create the data relation
I get an invalid constraint exception thrown that says:

Parent columns and child columns don't have type- matching columns'

Is there anyway of getting around this issue, other than
changing the types of the columns? I am linking to the
data using an MS Access database (the data is an ODBC source)
and so I can't change the type!

Even though the type of the column that MS Access has
linked to is Double, it does in fact only contain integer
values.

Many thanks,

Jason.
 

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