Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:07:48+00:00 2026-06-14T10:07:48+00:00

I had this method to insert new company to database but I have problem

  • 0

I had this method to insert new company to database but I have problem with List<Contacts> because I want to be able to insert multiple contact for company. Could someone help me?

enter image description here

public static bool AddNewCompany(Company company,List<Contacts> contact , Location local)
{
    // get a configured DbCommand object
    DbCommand comm = GenericDataAccess.CreateCommand();

    // Set the stored procedure name 
    comm.CommandText = "AddNewCompany";

    //create new parameter @CompanyName
    DbParameter param = comm.CreateParameter();
    param.ParameterName = "@CompanyName";
    param.Value = company.CompanyName;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @CompanyDetail 
    param = comm.CreateParameter();
    param.ParameterName = "@CompanyDetail";
    param.Value = company.CompanyDetail;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @ModifiedDate
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDate";
    param.Value = DateTime.Now;
    param.DbType = DbType.DateTime;
    comm.Parameters.Add(param);

    //Company Info
    foreach (var c in contact)
    {
        //create new parameter @LabelContactTypeID
        param = comm.CreateParameter();
        param.ParameterName = "@LabelContactTypeID";
        param.Value = c.LabelContactTypeID;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @ContactDetails
        param = comm.CreateParameter();
        param.ParameterName = "@ContactDetails";
        param.Value = c.ContactDetail;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @Status
        param = comm.CreateParameter();
        param.ParameterName = "@Status";
        param.Value = c.Status;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);

        //create new parameter @Notes
        param = comm.CreateParameter();
        param.ParameterName = "@Notes";
        param.Value = c.Notes;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    }




    //Company Info
    //create new parameter @Address
    param = comm.CreateParameter();
    param.ParameterName = "@Address";
    param.Value = local.Address;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @City
    param = comm.CreateParameter();
    param.ParameterName = "@City";
    param.Value = local.City;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Province
    param = comm.CreateParameter();
    param.ParameterName = "@Province";
    param.Value = local.Province;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @PostalCode
    param = comm.CreateParameter();
    param.ParameterName = "@PostalCode";
    param.Value = local.PostalCode;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @Note
    param = comm.CreateParameter();
    param.ParameterName = "@Note";
    param.Value = local.Note;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);

    //create new parameter @ModifiedDateLocation  
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDateLocation";
    param.Value = DateTime.Now;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);


    try
    {
        return (GenericDataAccess.ExecuteNonQuery(comm) != -1);

    }
    catch
    {
        return false;
    }
}

SQL Server stored procedure:

ALTER PROCEDURE [dbo].[AddNewCompany]
    @CompanyName nvarchar(50),
    @CompanyDetail nvarchar(max),
    @ModifiedDate datetime,
    --ContactInfo
    @LabelContactTypeID  int,
    @ContactDetails nvarchar(MAX),
    @Status bit,
    @Notes nvarchar(MAX),
    -- Company Location 
    @Address nvarchar(max),
    @City nvarchar(50),
    @Province nvarchar(50),
    @PostalCode nvarchar(10),
    @Note nvarchar(max),
    @ModifiedDateLocation datetime
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company] ([companyName],[companyDetail], [modifiedDate])
     VALUES (@CompanyName, @CompanyDetail, @ModifiedDate)

    DECLARE @CompanyID int 
    SET @CompanyID = SCOPE_IDENTITY();

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company_Contacts] ([companyID], [labelContactTypeID], [contactDetails], [status], [notes])
    VALUES (@CompanyID, @LabelContactTypeID, @ContactDetails, @Status, @Notes)

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Location]([address], [city], [province], [postalCode], [note], [modifiedDate])
    VALUES (@Address, @City, @Province, @PostalCode, @Note, @ModifiedDateLocation)          

    DECLARE @LocationID  INT 
    SET @LocationID = SCOPE_IDENTITY(); 

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company_location]([companyID], [locationID])
    VALUES (@CompanyID, @LocationID)            
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:07:49+00:00Added an answer on June 14, 2026 at 10:07 am

    Your stored procedure is inserting only one contact for the company. If you would like to add another contact (or more of them), write another procedure that will insert new contacts using the CompanyID from the last company inserted to the database.

    I can’t write you code because I don’t know the tables and can’t replicate the results, but you should create another procedure from this code

    INSERT INTO [TaskManagementSystem_DB].[dbo].[Company_Contacts]
               ([companyID] 
    
               ,[labelContactTypeID]
               ,[contactDetails]
               ,[status]
               ,[notes])
         VALUES
               (@CompanyID 
               ,@LabelContactTypeID
               ,@ContactDetails
               ,@Status
               ,@Notes)
    

    In the first part just add one contact from the list (the first one that is on index 0 in the list)

    Read the CompanyID of company that has been just inserted and for each other contact in the list, using this ID call your new procedure. It will add new contacts that are referenced to the company using the same companyID (referential identity one to many).

    EDIT You should do something like this:

    public static bool AddNewCompany(Company company,List<Contacts> contact , Location local)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
    //Set the store Proc name 
    comm.CommandText = "AddNewCompany";
    
    
    //create new parameter @CompanyName
    DbParameter param = comm.CreateParameter();
    param.ParameterName = "@CompanyName";
    param.Value = company.CompanyName;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @CompanyDetail 
    param = comm.CreateParameter();
    param.ParameterName = "@CompanyDetail";
    param.Value = company.CompanyDetail;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @ModifiedDate
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDate";
    param.Value = DateTime.Now;
    param.DbType = DbType.DateTime;
    comm.Parameters.Add(param);
    
        //Company Info
    //add only one contact
        /create new parameter @LabelContactTypeID
            param = comm.CreateParameter();
            param.ParameterName = "@LabelContactTypeID";
            param.Value = contact[0].LabelContactTypeID;
            param.DbType = DbType.StringFixedLength;
            comm.Parameters.Add(param);
    
        //create new parameter @ContactDetails
        param = comm.CreateParameter();
        param.ParameterName = "@ContactDetails";
        param.Value = contact[0].ContactDetail;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    
        //create new parameter @Status
        param = comm.CreateParameter();
        param.ParameterName = "@Status";
        param.Value = contact[0].Status;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    
        //create new parameter @Notes
        param = comm.CreateParameter();
        param.ParameterName = "@Notes";
        param.Value = contact[0].Notes;
        param.DbType = DbType.StringFixedLength;
        comm.Parameters.Add(param);
    
    
    //Company Info
    //create new parameter @Address
    param = comm.CreateParameter();
    param.ParameterName = "@Address";
    param.Value = local.Address;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @City
    param = comm.CreateParameter();
    param.ParameterName = "@City";
    param.Value = local.City;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @Province
    param = comm.CreateParameter();
    param.ParameterName = "@Province";
    param.Value = local.Province;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @PostalCode
    param = comm.CreateParameter();
    param.ParameterName = "@PostalCode";
    param.Value = local.PostalCode;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @Note
    param = comm.CreateParameter();
    param.ParameterName = "@Note";
    param.Value = local.Note;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    //create new parameter @ModifiedDateLocation  
    param = comm.CreateParameter();
    param.ParameterName = "@ModifiedDateLocation";
    param.Value = DateTime.Now;
    param.DbType = DbType.StringFixedLength;
    comm.Parameters.Add(param);
    
    
    try
    {
        return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
    
    }
    catch
    {
        return false;
    }
    

    }

    Create a new method that adds new contact using the part of the procedure I pasted here and insert all other contacts. Pas to this method a companyID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this weird problem a little while ago and haven't been able to
I had this problem some time ago and I gave up but lately it
This seems like it would have a really easy solution...but I've had a hard
Had this working; at one stage. The problem is the following text is now
I had this, which was working fine: public static Integer[] photos = new Integer[]
I had this problem before and can't for life of me remember how to
I had this nasty bug that disappeared in the past but now after quite
I had this error when I browse new web site that site sub from
I have this code. I receive several lists as parameters that I insert into
I'm quite new to Linq and have had some problems updating my dabase. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.