E
Earl
Ideally, what I would like to do is to populate a datagrid with the
information from 2 tables. Man, does that sound simple ... if so, please
join me on this expedition ...
Begin by using two dataAdapters, filling one from the main database table
and another from the foreign-keyed table and set up a data relation (thanks
to Kevin on pointing me in the general direction). Now use the expressions
to add columns that we want displayed in the grid.
But this is where things get crazy.
Indeed, I can now bind this related data to the datagrid. Everything "looks"
great. I can even do a delete with CurrencyManager. But when I try to do an
Insert to the main table, I get a constraint exception,
"ForeignKeyConstraint requires the child key values to exist in the parent
table"
Do who, huh, what? The child (Supplier) key values must exist in the parent
(Zip) table. My update logic calls only for the Supplier table (and worked
perfect before I went down this road). And I most certainly do not want to
be inserting any data into the Zip table (it is a lookup table for all zip,
city, state names in the U.S.).
Just because I enjoy banging my head against the wall, I set the relation
constraint to False. Now we actually add a record to the Supplier table, but
I still get an exception: "Cannot change ReadOnly property for the
Expression column"
Here is the relevant snippet. If anyone can see clearly through this, advice
would be appreciated. Note that what should be the Parent table is set as
the Child because the silly syntax for the Expression apparently does not
allow you to associate a column from the Child.
********************************************
Dim strSQLServer As New SqlConnection(strConnString)
Dim cmdSuppliers As New SqlCommand("GetAllSuppliers", strSQLServer)
'a join query that matches up all supplier zip IDs
Dim cmdZipMatches As New SqlCommand("GetAllSupplierZips", strSQLServer)
cmdSuppliers.CommandType = CommandType.StoredProcedure
cmdZipMatches.CommandType = CommandType.StoredProcedure
cmdSuppliers.Connection = strSQLServer
cmdZipMatches.Connection = strSQLServer
strSQLServer.Open()
Dim da As New SqlDataAdapter(cmdSuppliers)
Dim daZip As New SqlDataAdapter(cmdZipMatches)
da.Fill(dsx, "dtSuppliers")
daZip.Fill(ds, "dtSupplierZips")
ds.Tables("dtSuppliers").Columns("SupplierID").ColumnMapping =
MappingType.Hidden
ds.Tables("dtSuppliers").Columns("Tstamp").ColumnMapping =
MappingType.Hidden
ds.Tables("dtSuppliers").Columns("SupplierZipCityID").ColumnMapping =
MappingType.Hidden
ds.Tables("dtSupplierZips").Columns("ZipCityStateID").ColumnMapping =
MappingType.Hidden
Dim rel As DataRelation
rel = New DataRelation("CityStateZipSupplier", _
ds.Tables("dtSupplierZips").Columns("ZipCityStateID"), _
ds.Tables("dtSuppliers").Columns("SupplierZipCityID"), False)
ds.Relations.Add(rel)
ds("dtSuppliers").Columns.Add("City", GetType(String),
"Parent(CityStateZipSupplier).City")
ds.Tables("dtSuppliers").Columns.Add("State", GetType(String),
"Parent(CityStateZipSupplier).State")
ds.Tables("dtSuppliers").Columns.Add("Zip", GetType(String),
"Parent(CityStateZipSupplier).Zip")
dtSuppliers = ds.Tables("dtSuppliers")
DataGrid1.DataSource = dtSuppliers
information from 2 tables. Man, does that sound simple ... if so, please
join me on this expedition ...
Begin by using two dataAdapters, filling one from the main database table
and another from the foreign-keyed table and set up a data relation (thanks
to Kevin on pointing me in the general direction). Now use the expressions
to add columns that we want displayed in the grid.
But this is where things get crazy.
Indeed, I can now bind this related data to the datagrid. Everything "looks"
great. I can even do a delete with CurrencyManager. But when I try to do an
Insert to the main table, I get a constraint exception,
"ForeignKeyConstraint requires the child key values to exist in the parent
table"
Do who, huh, what? The child (Supplier) key values must exist in the parent
(Zip) table. My update logic calls only for the Supplier table (and worked
perfect before I went down this road). And I most certainly do not want to
be inserting any data into the Zip table (it is a lookup table for all zip,
city, state names in the U.S.).
Just because I enjoy banging my head against the wall, I set the relation
constraint to False. Now we actually add a record to the Supplier table, but
I still get an exception: "Cannot change ReadOnly property for the
Expression column"
Here is the relevant snippet. If anyone can see clearly through this, advice
would be appreciated. Note that what should be the Parent table is set as
the Child because the silly syntax for the Expression apparently does not
allow you to associate a column from the Child.
********************************************
Dim strSQLServer As New SqlConnection(strConnString)
Dim cmdSuppliers As New SqlCommand("GetAllSuppliers", strSQLServer)
'a join query that matches up all supplier zip IDs
Dim cmdZipMatches As New SqlCommand("GetAllSupplierZips", strSQLServer)
cmdSuppliers.CommandType = CommandType.StoredProcedure
cmdZipMatches.CommandType = CommandType.StoredProcedure
cmdSuppliers.Connection = strSQLServer
cmdZipMatches.Connection = strSQLServer
strSQLServer.Open()
Dim da As New SqlDataAdapter(cmdSuppliers)
Dim daZip As New SqlDataAdapter(cmdZipMatches)
da.Fill(dsx, "dtSuppliers")
daZip.Fill(ds, "dtSupplierZips")
ds.Tables("dtSuppliers").Columns("SupplierID").ColumnMapping =
MappingType.Hidden
ds.Tables("dtSuppliers").Columns("Tstamp").ColumnMapping =
MappingType.Hidden
ds.Tables("dtSuppliers").Columns("SupplierZipCityID").ColumnMapping =
MappingType.Hidden
ds.Tables("dtSupplierZips").Columns("ZipCityStateID").ColumnMapping =
MappingType.Hidden
Dim rel As DataRelation
rel = New DataRelation("CityStateZipSupplier", _
ds.Tables("dtSupplierZips").Columns("ZipCityStateID"), _
ds.Tables("dtSuppliers").Columns("SupplierZipCityID"), False)
ds.Relations.Add(rel)
ds("dtSuppliers").Columns.Add("City", GetType(String),
"Parent(CityStateZipSupplier).City")
ds.Tables("dtSuppliers").Columns.Add("State", GetType(String),
"Parent(CityStateZipSupplier).State")
ds.Tables("dtSuppliers").Columns.Add("Zip", GetType(String),
"Parent(CityStateZipSupplier).Zip")
dtSuppliers = ds.Tables("dtSuppliers")
DataGrid1.DataSource = dtSuppliers