Error when update data in dataGrid (urgent)

chin

New member
Joined
Mar 10, 2006
Messages
1
Programming Experience
Beginner
Hi, i create a dataGrid, which include update, edit, cancel function. But when i finished edit the data and click on update, it show me this error "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Source error:
Line 129: Dim updateCmd As String
Line 130:
Line 131: updateCmd = "UPDATE Admin SET " & "admin_username = @admin_username" & _
Line 132: ", admin_pwd = @admin_pwd" & _
Line 133: ", admin_name = @admin_name" & _

Below is my code for update the data:
Dim myConnection As SqlConnection
myConnection = New SqlConnection(ConfigurationSettings.AppSettings("cnn"))
Dim updateCmd As String

updateCmd = "UPDATE Admin SET " & "admin_username = @admin_username" & _
", admin_pwd = @admin_pwd" & _
", admin_name = @admin_name" & _
", admin_email = @admin_email" & _
" where admin_id = " & dgAdmin.DataKeys(CInt(e.Item.ItemIndex))
Dim myCommand As SqlCommand
myCommand = New SqlCommand(updateCmd, myConnection)
myCommand.Parameters.Add(New SqlParameter("@admin_username", SqlDbType.VarChar, 10))
myCommand.Parameters.Add(New SqlParameter("@admin_pwd", SqlDbType.VarChar, 20))
myCommand.Parameters.Add(New SqlParameter("@admin_name", SqlDbType.VarChar, 50))
myCommand.Parameters.Add(New SqlParameter("@admin_email", SqlDbType.VarChar, 50))
Dim txtUsername As System.Web.UI.WebControls.TextBox = e.Item.Cells(1).Controls(0)
Dim txtPwd As System.Web.UI.WebControls.TextBox = e.Item.Cells(2).Controls(0)
Dim txtName As System.Web.UI.WebControls.TextBox = e.Item.Cells(3).Controls(0)
Dim txtEmail As System.Web.UI.WebControls.TextBox = e.Item.Cells(4).Controls(0)

myCommand.Parameters("@admin_username").Value = txtUsername.Text
myCommand.Parameters("@admin_pwd").Value = txtPwd.Text
myCommand.Parameters("@admin_name").Value = txtName.Text
myCommand.Parameters("@admin_email").Value = txtEmail.Text
myCommand.Connection.Open()
Try
myCommand.ExecuteNonQuery()
lblResult.Text = "<b>ID " & dgAdmin.DataKeys(CInt(e.Item.ItemIndex)) & " is updated</b><br>"
dgAdmin.EditItemIndex = -1
Catch exp As Exception
lblResult.Text = "ERROR: " & exp.Message
End Try
myCommand.Connection.Close()
BindData("admin_id")

Thanks for anyone can give help to me..
 
Back
Top