Link to home
Start Free TrialLog in
Avatar of jenniferw
jenniferw

asked on

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?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Does ProductParameterGrid.Items.Count = dsProductParameter.ProductParameter.Rows.Count?

Bob
Avatar of jenniferw
jenniferw

ASKER

Yes.
Have you combined multiple modifications, deletes, updates into one update?

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

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

  dbcInterface.UpdateProductParameter(rows);

Does that work?

Bob
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.
My primary key, ProductParameterGlobalID, the row identifier contains numbers
greater than the size of the argument list. Could that be the problem?
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
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
What does the UpdateCommand.CommandText look like for sqlDaProductParameter?

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

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

Jennifer
Are you using the DataView to filter and sort?

Bob
Yes...Hmmm...
Are you thinking, scratching your head, pondering options, what?

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

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


ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank's for helping...