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?
 
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
0
 
Bob LearnedCommented:
Does ProductParameterGrid.Items.Count = dsProductParameter.ProductParameter.Rows.Count?

Bob
0
 
jenniferwAuthor Commented:
Yes.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Bob LearnedCommented:
Have you combined multiple modifications, deletes, updates into one update?

Bob
0
 
jenniferwAuthor Commented:
No, I'm just editing the cells in the datagrid, and calling update.
No deletes.
0
 
jenniferwAuthor Commented:
And no inserts....
0
 
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
0
 
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.
0
 
jenniferwAuthor Commented:
My primary key, ProductParameterGlobalID, the row identifier contains numbers
greater than the size of the argument list. Could that be the problem?
0
 
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
0
 
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
0
 
Bob LearnedCommented:
What does the UpdateCommand.CommandText look like for sqlDaProductParameter?

Bob
0
 
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
0
 
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
0
 
jenniferwAuthor Commented:
No...

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

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

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

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

Bob
0
 
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....


0
 
jenniferwAuthor Commented:
Thank's for helping...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.