Unable to Update with DataGrid


Troy Jerkins

I been trying (all day) to update a record with the DataGrid. .NetFramework
1.1 VS 2003 Ent.

Table Name (SQL Server 2000) Users:
Uid = int
UserName = varchar (255)
passwordHash = varchar (40)
salt = varchar (10)
Role = Varchar (40)
FName = varchar (50)
LName = varchar (50)

DataGrid Name = dataUsr
I run a StoredProcedure to get the columns Uid, UserName, Role, FName, LName

Here is my UpdateCommand Codebehind: (Template Columns)

Dim Uid As String = dataUsr.DataKeys(CType(e.Item.ItemIndex, Integer))

Dim txtRole As TextBox = CType(e.Item.Cells(3).FindControl("Role"),
Dim Role As String = txtRole.Text
Dim txtFName As TextBox =
CType(e.Item.Cells(4).FindControl("FName"), TextBox)
Dim FName As String = txtFName.Text
Dim txtLName As TextBox =
CType(e.Item.Cells(5).FindControl("LName"), TextBox)
Dim LName As String = txtLName.Text
Dim txtUserName As TextBox =
CType(e.Item.Cells(6).FindControl("UserName"), TextBox)
Dim UserName As String = txtUserName.Text

Dim DS As DataSet
Dim objDB As New DBAccess.DAOObject
Dim sample As Integer
' Run the StoredProcedure to Update the Table.
sample = objDB.RunSPReturnInteger("UpdateUsers", New
SqlParameter("@Uid", Uid), New SqlParameter("@Role", Role), New
SqlParameter("@FName", txtFNameBox.Text), New
SqlParameter("@FName", txtFNameBox.Text), New SqlParameter("@LName",
txtLNameBox.Text), New SqlParameter("@UserName", txtUserNameBox.Text))
dataUsr.EditItemIndex = -1

Each time I receive the error:

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index

Index is always 0 when debugging. What am I doing wrong!!!!!!!!!!!!!!!!!!



I had a similar problem. You might want to try this -- the sample code on
MSDN leaves out a key bit: You have to explicitly assign a key field prior
to binding, even though the dataset may appear to have already assigned it
for you.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataKeyField() = "MyPrimaryKeyFieldName"
End If
End Sub

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question