Click here to Skip to main content
14,937,188 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
  Goals:

Insert new records from the employee bound datagridview. Populating and inserting a related contact

Either by clicking on the new row or by pressing the "+" button on the bindingNavigator.

Tables:

employee 

 

EmployeeID (PK, auto)

FirstName

 

contact

 

contactID (PK, auto)

EmployeeID

Address  

Parent employee.EmployeeID --> contact.EmployeeID (one-to-many)

BindingNavigatorAddNewItem_Click event: 

 private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            employee employees = new employee
            {

                contacts = new System.Data.Linq.EntitySet<contact>()
                {
                    new contact{    PrimaryContact = 1,
                                    Address = addressTextBox.Text,
                                    City = cityTextBox.Text,
                                    Province = provinceTextBox.Text,
                                    Country = countryTextBox.Text,
                                    PostalCode = postalCodeTextBox.Text,
                                    Phone = phoneTextBox.Text,
                                    Mobil = mobilTextBox.Text,
                                    Fax = faxTextBox.Text,
                                    Email = emailTextBox.Text
                    }
                }
            };

            context.employees.InsertOnSubmit(employees);
            
        }  

 The Debug output of this is:  

 INSERT INTO [dbo].[employee]([UserName], [Password], [Photo], [CreatedDate], [Salutation], [FirstName], [MiddleName], [LastName], [NickName], [PreferredName], [BirthDate], [Age], [Citizenship], [Sex], [Ethnicity], [SIN], [DriversLicenceNumber], [MaritalStatus], [MaidenName], [Comment], [DeceasedID], [SupervisorID])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19, @p20, @p21)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p1: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p2: Input Image (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p3: Input Date (Size = 0; Prec = 0; Scale = 0) []
-- @p4: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p5: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p6: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p7: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p8: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p9: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p10: Input Date (Size = 0; Prec = 0; Scale = 0) []
-- @p11: Input Int (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p12: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p13: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p14: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p15: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p16: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p17: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p18: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p19: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p20: Input Int (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p21: Input Int (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.715

INSERT INTO [dbo].[employee]([UserName], [Password], [Photo], [CreatedDate], [Salutation], [FirstName], [MiddleName], [LastName], [NickName], [PreferredName], [BirthDate], [Age], [Citizenship], [Sex], [Ethnicity], [SIN], [DriversLicenceNumber], [MaritalStatus], [MaidenName], [Comment], [DeceasedID], [SupervisorID])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19, @p20, @p21)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p1: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p2: Input Image (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p3: Input Date (Size = 0; Prec = 0; Scale = 0) []
-- @p4: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p5: Input VarChar (Size = 15; Prec = 0; Scale = 0) [Test First Name]
-- @p6: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p7: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p8: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p9: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p10: Input Date (Size = 0; Prec = 0; Scale = 0) []
-- @p11: Input Int (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p12: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p13: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p14: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p15: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p16: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p17: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p18: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p19: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p20: Input Int (Size = 0; Prec = 0; Scale = 0) [Null]
-- @p21: Input Int (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.715

INSERT INTO [dbo].[contact]([PrimaryContact], [Address], [City], [Province], [Country], [PostalCode], [Phone], [Mobil], [Fax], [Email], [EmployeeID], [BusinessDetailID])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11)

SELECT [t0].[ContactID], [t0].[EmployeeID], [t0].[BusinessDetailID]
FROM [dbo].[contact] AS [t0]
WHERE [t0].[ContactID] = (SCOPE_IDENTITY())
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p2: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p3: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p4: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p5: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p6: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p7: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p8: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p9: Input VarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p10: Input Int (Size = 0; Prec = 0; Scale = 0) [63]
-- @p11: Input Int (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.715

 

 As you can see the Add New button first inserts a record that is related to the columns in the datagridview.

Then inserts a new record based on the object created inside the click event. The object inside the click event works for updating parent and child data.

 

I have been searching for an answer to this for some time now. I have read as many related posts and am still unable to get the results that I am looking for.

 Thanks in advance

Posted
Updated 16-Nov-09 14:43pm
v7

1 solution

I don't have an answer for you, but I do have a suggestion that might help you get the answer.

1. Your question currently reads like a massive wall of text and unformatted code. Try slimming down your post, include only the essentials. Brevity is a virtue.

2. There's no real bottom-line question. Or if there is, it's lost in a sea of text. Make sure your question actually stands out, perhaps by bolding it, and make your question as straightforward as possible.  

   
v2

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