Unable to Update with DataGrid

  • Thread starter Thread starter Troy Jerkins
  • Start date Start date
T

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:
Columns:
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
only.

Here is my UpdateCommand Codebehind: (Template Columns)

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

Dim txtRole As TextBox = CType(e.Item.Cells(3).FindControl("Role"),
TextBox)
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
BindGrid()
END Sub

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!!!!!!!!!!!!!!!!!!
HELP!

-Troy
 
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
SQLDataAdapter1.Fill(DataSet1)
If Not IsPostBack Then
DataGrid1.DataKeyField() = "MyPrimaryKeyFieldName"
DataGrid1.DataBind()
End If
End Sub
 
Back
Top