Problems with DataAdapter.Update ( )

When I push the Edit DataGrid button above the DataGrid, I get a DataGrid where every row and cell are
in EditMode.

But when I push the Update DataGrid button above the dataGrid, only the first row in the dataset is
updated in the database, and then a a System.FormatExeption, saying: "Index (zero based) must be greater
than or equal to zero and less than the size of the argument list." is thrown.

My button update code is as follows:

public void UpdateDataGrid_Click(object sender, System.EventArgs e)
{
            try
            {
                DataRowCollection dr;
                DataTable dt = dsProductParameter.ProductParameter;
                dr = dt.Rows;

                for (int i = 0; i < ProductParameterGrid.Items.Count; i++)
                {
                    // Copy values from datagrid
                    dr[i]["ID"] = ((TextBox)ProductParameterGrid.Items[i].Cells[2].Controls[3]).Text;
                    dr[i]["Name"] = ((TextBox)ProductParameterGrid.Items[i].Cells[3].Controls[3]).Text;
                    dr[i]["Description"] = ((TextBox)ProductParameterGrid.Items[i].Cells[4].Controls[3]).Text;
                    dr[i]["PropertyValue"] = ((TextBox)ProductParameterGrid.Items[i].Cells[5].Controls[3]).Text;
                    dr[i]["PropertyValueDataType"] = ((TextBox)ProductParameterGrid.Items[i].Cells[6].Controls[3]).Text;
                    dr[i]["PropertyValueUnitOfMeasure"] = ((TextBox)ProductParameterGrid.Items[i].Cells[7].Controls[3]).Text;
                }

                    dbcInterface.UpdateProductParameter(dsProductParameter);

                     ..........
}

My UpdateProductParameter( ) method code that fails is :

public void UpdateProductParameter(DSProductParameter ds)
{
     try
     {
           sqlDaProductParameter.Update(ds);
     }
     catch (System.Exception Se)
     {
                      .......
                }
}

What am I doing wrong?
jenniferwAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Does ProductParameterGrid.Items.Count = dsProductParameter.ProductParameter.Rows.Count?

Bob
jenniferwAuthor Commented:
Yes.
Bob LearnedCommented:
Have you combined multiple modifications, deletes, updates into one update?

Bob
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

jenniferwAuthor Commented:
No, I'm just editing the cells in the datagrid, and calling update.
No deletes.
jenniferwAuthor Commented:
And no inserts....
Bob LearnedCommented:
This is just a test:

  DataRow[] rows = new DataRow[dt.Rows.Count];
  dt.Rows.CopyTo(rows, 0);

  dbcInterface.UpdateProductParameter(rows);

Does that work?

Bob
jenniferwAuthor Commented:
I don't have access to the web server, so I'm not able to test it today...

But, why don't it work with a DataSet? What is making the index in the DataSet?
And what is the index in the DataRow[] rows.
jenniferwAuthor Commented:
My primary key, ProductParameterGlobalID, the row identifier contains numbers
greater than the size of the argument list. Could that be the problem?
Bob LearnedCommented:
It shouldn't matter what the values for the primary key column, since it is an argument index out-of-range exception that you are getting.

Bob
jenniferwAuthor Commented:
I have tested:

  DataRow[] rows = new DataRow[dt.Rows.Count];
  dt.Rows.CopyTo(rows, 0);

  dbcInterface.UpdateProductParameter(rows);

It doesn't solve the problem...

Jennifer
Bob LearnedCommented:
What does the UpdateCommand.CommandText look like for sqlDaProductParameter?

Bob
jenniferwAuthor Commented:
Here it comes.....

UPDATE [ProductParameter] SET [ProductSegmentGlobalId] = @ProductSegmentGlobalId, [ID] = @ID, [Name] = @Name, [Description] = @Description, [PropertyValue] = @PropertyValue, [PropertyValueDataType] = @PropertyValueDataType, [PropertyValueUnitOfMeasure] = @PropertyValueUnitOfMeasure, [LastModified] = @LastModified WHERE (([ProductParameterGlobalId] = @Original_ProductParameterGlobalId) AND ([ProductSegmentGlobalId] = @Original_ProductSegmentGlobalId) AND ([ID] = @Original_ID) AND ((@IsNull_Name = 1 AND [Name] IS NULL) OR ([Name] = @Original_Name)) AND ((@IsNull_Description = 1 AND [Description] IS NULL) OR ([Description] = @Original_Description)) AND ((@IsNull_PropertyValue = 1 AND [PropertyValue] IS NULL) OR ([PropertyValue] = @Original_PropertyValue)) AND ((@IsNull_PropertyValueDataType = 1 AND [PropertyValueDataType] IS NULL) OR ([PropertyValueDataType] = @Original_PropertyValueDataType)) AND ((@IsNull_PropertyValueUnitOfMeasure = 1 AND [PropertyValueUnitOfMeasure] IS NULL) OR ([PropertyValueUnitOfMeasure] = @Original_PropertyValueUnitOfMeasure)) AND ([LastModified] = @Original_LastModified));
SELECT ProductParameterGlobalId, ProductSegmentGlobalId, ID, Name, Description, PropertyValue, PropertyValueDataType, PropertyValueUnitOfMeasure, LastModified FROM ProductParameter WHERE (ProductParameterGlobalId = @ProductParameterGlobalId) ORDER BY ProductParameterGlobalId

Jennifer
Bob LearnedCommented:
Pretty straight-forward stuff.  I can't see any explanation for your problem.  Are you maybe exceeding the max length for a string column?

Bob
jenniferwAuthor Commented:
No...

But I'm using a dataView upon the dataset, perhaps there is a problem with that?

Jennifer
Bob LearnedCommented:
Are you using the DataView to filter and sort?

Bob
jenniferwAuthor Commented:
Yes...Hmmm...
Bob LearnedCommented:
Are you thinking, scratching your head, pondering options, what?

Bob
jenniferwAuthor Commented:
Yes....
Bob LearnedCommented:
Ok, when you're done and you want to share some information with me, just let me know ;)

Bob
jenniferwAuthor Commented:
Well, then I'm back.
Now it's working, but i don't fully understand why.
Have a look at this code:

public void UpdateDataGrid_Click(object sender, System.EventArgs e)
{
    try
    {

        DataRowCollection ds = dsProductParameter.ProductParameter.Rows;
        DataRowCollection dv = dataViewProductParameter.ToTable().Rows;

        for (int i = 0; i < ds.Count; i++)
        {
            for (int j = 0; j < dv.Count; j++)
            {

                if (dv[i]["ProductParameterGlobalID"] == ds[j]["ProductParameterGlobalID"])
                {
                   // Copy values from datagrid to dataset
                   ds[j]["ID"] = ((TextBox)ProductParameterGrid.Items[i].Cells[2].Controls[3]).Text;
                   ds[j]["Name"] = ((TextBox)ProductParameterGrid.Items[i].Cells[3].Controls[3]).Text;
                   ds[j]["Description"] = ((TextBox)ProductParameterGrid.Items[i].Cells[4].Controls[3]).Text;
                   ds[j]["PropertyValue"] = ((TextBox)ProductParameterGrid.Items[i].Cells[5].Controls[3]).Text;
                   ds[j]["PropertyValueDataType"] = ((TextBox)ProductParameterGrid.Items[i].Cells[6].Controls[3]).Text;
                   ds[j]["PropertyValueUnitOfMeasure"] = ((TextBox)ProductParameterGrid.Items[i].Cells[7].Controls[3]).Text;
                 }
            }

            dbcInterface.UpdateProductParameter(dsProductParameter);
     }
     ................etc.........................

First I take care of the view/set sorting problem.
Than I'm running the update method for every loop.
I don't fully understand why I have to do that, perhaps you could help?
I wanted to fill the dataSet with new information and call the update method once.
But the FormatException is thrown....


Bob LearnedCommented:
If the DataGrid is bound to the DataView, then you need to process those rows, and not the complete set of rows for the entire DataTable.

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jenniferwAuthor Commented:
Thank's for helping...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.