Data grid update problem - index out of range

All, I'm new to .Net and ASP (previously worked in Access using VBA and ADO so everything is totally different and confusing!!)....

I've got a web page with a data grid on it. I used the Microsoft Help walkthroughs to generate an update command to update data in my Access database. This works on another page with a primary key that is of type string. However when I try to update my current page I get an Index out of range error, and I'm sure it's because the primary key is of type integer, everything else is the same, but I don't know how to fix it. My code is as follows:

Private Sub TypeDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles TypeDataGrid.UpdateCommand

Dim Type, RelatesTo, TypeID As String

' Gets the value of the key field of the row being updated

Dim key As String = TypeDataGrid.DataKeys(e.Item.ItemIndex).ToString()

' Gets get the value of the controls (textboxes) that the user

' updated. ' The first columns -- Cells(0 & 1) -- contains the Update/Cancel & Delete buttons.

Dim tb As TextBox

' Gets the value of the TextBox control in the columns

tb = CType(e.Item.Cells(2).Controls(0), TextBox)

RelatesTo = tb.Text

tb = CType(e.Item.Cells(3).Controls(0), TextBox)

Type = tb.Text

tb = CType(e.Item.Cells(4).Controls(0), TextBox)

TypeID = tb.Text

' Finds the row in the dataset table that matches the

' one the user updated in the grid. This example uses a

' special Find method defined for the typed dataset, which

' returns a reference to the row.

Dim r As TypeDataSet1.TypesRow

r = TypeDataSet1.Types.FindByTypeID(key)

' Updates the dataset table.

r.RelatesTo = RelatesTo

r.Type = Type

r.TypeID = TypeID

' Calls a SQL statement to update the database from the dataset

TypeDataAdapter.Update(TypeDataSet1)

' Takes the DataGrid row out of editing mode

TypeDataGrid.EditItemIndex = -1

' Refreshes the grid

TypeDataGrid.DataBind()

End Sub

Can anyone give me any clues

Also, on the same subject.... The primary key is an autonumber field, and when I've fixed the above problem I will be hiding the column. This is fine for read/edit and delete, however it will cause a problem when I add an Add facility. Has anyone any tips on how I should plan for this

Thanks!!

Julia




Answer this question

Data grid update problem - index out of range

  • Captain Kenny

    Excellent, thanks for your help on that! Any ideas on the first problem

  • two_man_only

    As I said, key and TypeID should be declared as integer instead of string because the field is the integer type.

  • RSVS2005

    key and TypeID should be declared as integer instead of string.

    Regarding your 2nd question, you should be able to set the AutoIncrement of the key column to be true. Then you can add a null object to tthat field and the actual value can be auto generated. In fact, even if you set some arbitrary value of that field, when you update the backend database, the actual written value will still folllow its identity property.



  • Data grid update problem - index out of range