Inserting only last row.

India Programmer

India Programmer

Sep 24 2010 7:15 AM

In data grid there are two fields one is itemcombox and another one is quantity(this is Grid text box type). All the items will be listed in itemcombo box which will come from database item field, this I have done already. My requirement is user can select many items and can enter quanity values in the grid cell. And these items and quantity will be saved in database table. In my code its saving only last row. Pls help somebody to achieve this.

private void btnSave_Click(object sender, EventArgs e)



        CompanyKey companyKey;

        Context context;

        SalesBackorder salesBackorder;

        Policy salesBackorderCreatePolicy;


        // Create an instance of the service

        DynamicsGP wsDynamicsGP = new DynamicsGP();


        // Be sure the default credentials are used

        wsDynamicsGP.UseDefaultCredentials = true;


        // Create a context with which to call the service

        context = new Context();


        // Specify which company to use (sample company)

        companyKey = new CompanyKey();

        companyKey.Id = (-1);


        // Set up the context object

        context.OrganizationKey = (OrganizationKey)companyKey;


        // Create a sales backorder object

        salesBackorder = new SalesBackorder();


        // Create a document type key for the sales backorder

        SalesDocumentTypeKey salesBackorderType = new SalesDocumentTypeKey();

        salesBackorderType.Type = SalesDocumentType.Backorder;


        // Populate the document type key of the sales backorder object

        salesBackorder.DocumentTypeKey = salesBackorderType;


       // Create a sales backorder line to specify the backordered item

        SalesBackorderLine salesBackorderLine = new SalesBackorderLine();


        // Create an item key

        ItemKey itemKey = new ItemKey();


        itemKey.Id = itemnumber;


        // Populate the item key property of the sales invoice line object

        salesBackorderLine.ItemKey = itemKey;


        // Create a sales backorder quantity object

        Quantity backorderCount = new Quantity();

        backorderCount.Value = Convert.ToDecimal(quantity);


        // Populate the quantity of the sales backorder line object

        salesBackorderLine.Quantity = backorderCount;



//SalesBackorderLine[] backorderLines = { salesBackorderLine };

        // Add the sales backorder line array to the sales backorder object

        //salesBackorder.Lines = backorderLines;



Note: I know here I should loop thru….


for (int counter = 0; counter < (CreateSalesBackOrderGrid.Rows.Count); counter++)



  Note: I am not clear how to handle this part. Pls help with this part.



           //Get the create policy for sales backorder objects

            salesBackorderCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesBackorder", context);

            // Create the sales backorder

            wsDynamicsGP.CreateSalesBackorder(salesBackorder, context, salesBackorderCreatePolicy);




            for (int i = 0; i <= CreateSalesBackOrderGrid.Rows.Count - 1; i++)






          catch (Exception ex)










 see if if uncomment

//SalesBackorderLine[] backorderLines = { salesBackorderLine };

        // Add the sales backorder line array to the sales backorder object

        //salesBackorder.Lines = backorderLines;

then only last row will be added otherwise in above code nothing will be saved….i have problem with the for loop before calling the insert method.

Answers (1)