Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Identity problem in inserting data into master and detail tables
 

Identity problem in inserting data into master and detail tables

I'm using the TableAdapters to insert rows into the master and detail tables. I would like to know what's the best practise to handle the identity column which is a auto-generate number.

As I'm required to insert all rows in a single transaction, I don't have the id no in the master table when I'm inserting data into the detail table. What's the simpliest way, or the best way, to insert these rows into the tables? I've already setup the relation between these tables but the insert process seems separate and can't match the id. column automatically.

cwlaualex  Wednesday, April 26, 2006 8:46 AM

Not sure if this answers you question, but here is an example on creating master-detail forms:

http://msdn2.microsoft.com/library/803kw7az(en-us,vs.80).aspx

When I did the example (not using an autonumber as I didn't have an example I could use), what basically happened is the data member of the parent table was the dataset, while the data member of the child table was the parent tables binding source (as opposed to the dataset). Maybe that can get you started in the rightdirection.

steveareno  Monday, May 01, 2006 2:56 AM
It's only a general introduction without any detail I needed. Anyway, thanks for your input.
cwlaualex  Thursday, May 04, 2006 2:57 AM
If you insert rows directly to a db table, look at SCOPE_IDENTITY() function (Transact-SQL)which returns the last identity value inserted into an identity column in the same scope. So after you insert a record into the master table you can get its ID for records in the detail table.
vkh75  Thursday, May 04, 2006 8:09 AM

I've been wrestling with the same problem. I have a parent child that will essentually be a one to zero or one relationship. I thought that it would be easy to have individual fields on the same form.

The form works fine for viewing and updating existing data, so I conclude that I have the relationship set up correctly. If I manually insert a child row I can modify it.

I would prefer to not use SQL to insert directly into a table, but to use the binding source. Any suggestions?

jphoekstra  Friday, May 05, 2006 4:01 PM

Is it possible to create a small table to handle the DocNo ?

I would handle like this, create a table such as I name it "Trpx",

My suggest is write a few code to "auto-generate" the next no

Doctype NextDocNo

Quot No

Try
Me.TrpxTableAdapter.Fill(Me.MyTradeDataSet.Trpx, "QUOT")
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
QuotNoTextBox.Text = DocTypeTextBox.Text & NextDocNoTextBox.Text
Me.Validate()
Me.QuoHeaderBindingSource.EndEdit()
Me.QuoHeaderTableAdapter.Update(Me.MyTradeDataSet.QuoHeader)

Dim NoIncrement As Integer = Integer.Parse(NextDocNoTextBox.Text)
NoIncrement = NoIncrement + 1
NextDocNoTextBox.Text = NoIncrement.ToString

Me.TrpxBindingSource.EndEdit()
Me.TrpxTableAdapter.Update(Me.MyTradeDataSet.Trpx)

The Detail table have a auto-id field by database and a foreign key refer to master, whatever, no use at all

So at the Save button update the detail , Select the current ID , after update the master and detail , add int 1 to the NextDocNo,

Is it what you need ? I hope it useful. I am a new learner in 2005, haha,

Also from HKSAR

Turbosolo  Wednesday, May 10, 2006 3:32 PM

You can use google to search for other answers

Custom Search

More Threads

• Image Reading Problem
• refresh the data table disable to select
• How do you refresh a DatGridView when the data source changes
• how to catch row modification hwn user move to a dfferent row for windwos form detail view
• Restrict datagrid for only one new row?
• data source configuration wizard
• can datagridview bind to a mysql database??
• First event fires by pressing delete key on datagrid?
• Automatic Cells in Datagrid
• Bind to a custom UserControl to a BindingSource