Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all visitors
i have 2 tables. Table SaleHeader is a master table and Table SaleDetail is a detail table.In master table, there is a primary key(SaleCode) and it is a foreign key in detail table.In detail table, there is a primary key(SaleDetailCode). So in detail table, there are one foreign key(SaleCode) and primary key(SaleDetailCode).
when i save, i want to save all data in master table to Table(SaleHeader) and all detail data(use DataGridView to show all data) to SaleDetail table.
in my source code is:
MSIL
Me.SaleHeaderBS.EndEdit()
Me.SaleHeaderTA.Update(Me.VinaSaleDS.SalesHeader)
Me.SaleDetailBS.Current("Salecode") = Me.txtDocnum.Text.Trim
Me.SaleDetailBS.EndEdit()
Me.SaleDetailTA.Update(Me.VinaSaleDS.SalesDetail)


but it error and show me the message that "SaleDetailCode couldn't null".
Note:
for SaleCode in Master Table, i use text box to input Sale Code.
for SaleDetailCode i hide in DataGrid, but i want it automatically increase coz SaleDetailCode autonumber in table.

Please help me about how i can save data from master and detail to tables.

Best Regards,
TONY
Posted
Comments
Karwa_Vivek 19-May-11 5:29am    
hi TONY
is your problem solved
by the solution what is provided by Mr S Mewara.

for SaleDetailCode i hide in DataGrid, but i want it automatically increase coz SaleDetailCode autonumber in table.
This has to do with table design and not via code.

Make it an Identity column[^] with a defined increment step.

It would be something like:
1. Go to 'Design Table'
2. Select the column of 'SaleDetailCode'
3. See at the properties of the column below.
4. There is an option of making it if 'Identity' column - mark it yes
5. You can also define the increment to any number. By default it is 1.
 
Share this answer
 
With Addition To
the process what is Told by S Mewara
that means
after marking the SaleDetailCode as An Identity Increament To One
You can use The max Function
to Increase the SaleDetailCode as Follows
and Call this function after you Click Your Save button
or Whenever you load the Form..
Public Sub Max_Id()
        Dim sd As New SqlDataAdapter("Select max(SaleDetailCode) from SaleDetail", myCon.con())
        Dim dt As New DataTable
        sd.Fill(dt)
        If (dt.Rows(0)(0).ToString() <> Nothing) Then
            txtEid.Text = Convert.ToInt32(dt.Rows(0)(0).ToString()) + 1
        Else
            txtEid.Text = "1"

        End If
    End Sub


Also I will Suggest You to use a Textbox in
Place Of datagrid to Input the SaleDetailCode
and then hide The Textbox
here I have Provided the same Taking a TextBox named txtEid and myCon is the connection Class What i am using.
use the Connection what you have Taken
 
Share this answer
 
Comments
soeun tony 19-May-11 7:34am    
Thanks for your reply.
if do like mean that in the datagrid can input only one time,i mean that normally for detail the user can input many time.Ex:detail table has ItemCode,ItemName, so the user can input 3 times of item in datagridview so SaleDetailCode also increase 3 automatically too. and when the user click on Save Button,The system will save Master Data and Detail(in GrideView 3 rows) to each table.
Best Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900