|
-
Mar 27th, 2009, 04:28 PM
#1
Thread Starter
New Member
Child tables primary Key not generated automatically
(VS2005 Professional)
My database's layout is as follows:
Identification (Parent table)
ID# (Primary Key, auto increase)
First Name
Last Name
....
Address (child table)
ID# (FK constraint to parent table)
AddressID(PK, auto increase)
Block #
StreetName
...
Vehicles(child table)
ID# (FK constraint to parent table)
VehiclesID(PK, auto increase)
color
....
I am placing this three tables in three tabs by a tabcontrol. If I put two child tables in dataGridView in their tab, the insert(new record)/update to the tables works very well. When I put the child tables in details View, I can't get a new record updated. The problem with details view is that there is no AddressID and VehiclesID generated. In dataGridView, these IDs are automatically generated whenever a mouse enters into a cell in a row.
Would someone please help? Please let me know if you need more details on my project.
Thank you!
-
Mar 27th, 2009, 09:32 PM
#2
Re: Child tables primary Key not generated automatically
I'm no ASP.NET expert but I would think that the problem is that a DataGridView in a WinForms app has a direct connection to the DataTable via the Binding object. In an ASP.NET app the controls have no connection to the data source. The data is extracted from the data source when the page is built and then that data is served to the user as HTML. The DataTable can't generate a new ID until the page is posted back to the server. I don't know as I've never used a DetailsView, but maybe that doesn't happen when you create a new record but only when you save it.
-
Mar 31st, 2009, 08:48 AM
#3
Thread Starter
New Member
Re: Child tables primary Key not generated automatically
Thank you. Yes, I agree. DataGridView must have a direct connection to the DataTable via the binding object.
In a DetailsView, a new ID can be generated through the "new" button in BindingNavigator. However, since the bindingNavigator is configured to the binding source for parent data table, the child table(s) can not get a new ID. Is there a way in coding the AddNewItem_Click for the "new" button to create a new record in child tables?
Thanks in advance!
-
Apr 3rd, 2009, 10:10 AM
#4
Thread Starter
New Member
Re: Child tables primary Key not generated automatically
This is what I did to get a new Primary key for the child table. It worked, but I would like to know if this is the best way to do it.
The steps are:
1. Get a new record for the master table (get a new Master table PK) through the new button in Binding Navigator.
2. Add/update a new row to the master table.
3. Insert Master PK into child table. During the insert process, a new child PK is obtained.
4. Update child table row.
5. Refresh the tables.
Code:
If Me.Validate Then
Me.MainBindingSource.EndEdit()
Me.AddressBindingSource.EndEdit()
Me.VehiclesBindingSource.EndEdit()
End If
Dim mainUpdates() As DataRow = Me.SORTSDataSet.Main.Select("", "", DataViewRowState.Added)
Me.MainTableAdapter.Update(mainUpdates)
Try
Dim con As SqlConnection = New SqlConnection("Data Source=myDataSource;Initial Catalog=myCatalog;Integrated Security=True")
con.Open()
Dim cmd1 As SqlCommand
cmd1 = New SqlCommand("Insert Into address(MainID) Values('" & MainIDTxtBox.Text & "' )", con)
cmd1.ExecuteNonQuery()
Dim cmd2 As SqlCommand
cmd2 = New SqlCommand("Insert Into Vehicles(MainID) values('" & MainIDTxtBox.Text & "')", con)
cmd2.ExecuteNonQuery()
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Dim addressUpdates() As DataRow = Me.MainDataSet.Address.Select("", "", DataViewRowState.Added)
Dim VehiclesUpdates() As DataRow = Me.MainDataSet.Vehicles.Select("", "", DataViewRowState.Added)
Me.AddressTableAdapter.Update(addressUpdates)
Me.VehiclesTableAdapter.Update(VehiclesUpdates)
Me.AddressTableAdapter.Fill(Me.MainDataSet.Address)
Me.VehiclesTableAdapter.Fill(Me.MainDataSet.Vehicles)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|