Index was out of range ERROR <NEWBIE ERROR>

G

Guest

Hello all!
I'm just trying to delete a row from in a datagrid. Getting the following
error.
"System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name: index"

Here is my code,

Public Class frmNotesGrd
Inherits System.Web.UI.Page
Protected ConnString As String =
ConfigurationSettings.AppSettings("ConnString")

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.daNoteGrd = New System.Data.SqlClient.SqlDataAdapter
Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
Me.ConnNoteGrd = New System.Data.SqlClient.SqlConnection
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
Me.DsNoteGrd1 = New NConline.dsNoteGrd
CType(Me.DsNoteGrd1,
System.ComponentModel.ISupportInitialize).BeginInit()
'
'daNoteGrd
'
Me.daNoteGrd.InsertCommand = Me.SqlInsertCommand1
Me.daNoteGrd.SelectCommand = Me.SqlSelectCommand1
Me.daNoteGrd.TableMappings.AddRange(New
System.Data.Common.DataTableMapping() {New
System.Data.Common.DataTableMapping("Table", "Notes", New
System.Data.Common.DataColumnMapping() {New
System.Data.Common.DataColumnMapping("NoteID", "NoteID"), New
System.Data.Common.DataColumnMapping("ToUser", "ToUser"), New
System.Data.Common.DataColumnMapping("Body", "Body"), New
System.Data.Common.DataColumnMapping("Frm", "Frm"), New
System.Data.Common.DataColumnMapping("Date", "Date")})})
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.CommandText = "INSERT INTO Notes(ToUser, Body,
Frm, Date) VALUES (@ToUser, @Body, @Frm, @Date); " & _
"SELECT NoteID, ToUser, Body, Frm, Date FROM Notes"
Me.SqlInsertCommand1.Connection = Me.ConnNoteGrd
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@ToUser", System.Data.SqlDbType.NVarChar,
100, "ToUser"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Body", System.Data.SqlDbType.NVarChar,
200, "Body"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Frm", System.Data.SqlDbType.NVarChar,
100, "Frm"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Date", System.Data.SqlDbType.DateTime,
8, "Date"))
'
'ConnNoteGrd
'
Me.ConnNoteGrd.ConnectionString = "workstation id=LOCALHOST;packet
size=4096;user id=Tesla;data source=NCONLINE;pers" & _
"ist security info=False;initial catalog=Forums"
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT NoteID, ToUser, Body,
Frm, Date FROM Notes"
Me.SqlSelectCommand1.Connection = Me.ConnNoteGrd
'
'DsNoteGrd1
'
Me.DsNoteGrd1.DataSetName = "dsNoteGrd"
Me.DsNoteGrd1.Locale = New System.Globalization.CultureInfo("en-US")
CType(Me.DsNoteGrd1,
System.ComponentModel.ISupportInitialize).EndInit()

End Sub
Protected WithEvents daNoteGrd As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Protected WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
Protected WithEvents ConnNoteGrd As System.Data.SqlClient.SqlConnection
Protected WithEvents DsNoteGrd1 As NConline.dsNoteGrd
Protected WithEvents dgNotes As System.Web.UI.WebControls.DataGrid
Protected WithEvents lblUser As System.Web.UI.WebControls.Label
Protected WithEvents lblPagingInfo As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblUser.Text = Session("UserNm")
If Not Page.IsPostBack Then
BindData()
End If






End Sub

Sub BindData()
ConnNoteGrd = New SqlConnection(ConnString)
Dim strQse As String
strQse = "SELECT * FROM Notes Where ToUser ='" + lblUser.Text + "'
ORDER by Date"
Dim cmdNoteGrd As New SqlCommand(strQse, ConnNoteGrd)

Dim daNotegrd As New SqlDataAdapter
daNotegrd.SelectCommand = cmdNoteGrd
ConnNoteGrd.Open()

Dim dsNoteGrd As DataSet = New DataSet
daNotegrd.Fill(dsNoteGrd, "Notes")

ConnNoteGrd.Close()

dgNotes.DataSource = dsNoteGrd
dgNotes.DataBind()

ConnNoteGrd.Close()

End Sub
Sub ShowPageInfo()
'This sub didsplays pageing info in apropiate label
lblPagingInfo.Text = "DispalyingPage " & (dgNotes.CurrentPageIndex +
1).ToString() & " of " & dgNotes.PageCount
End Sub


Private Sub dgNotes_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgNotes.DeleteCommand

dgNotes.EditItemIndex = -1

Dim NoteID As Integer
NoteID = dgNotes.DataKeys(e.Item.ItemIndex)

Dim strDelete As String
strDelete = "DELETE FROM Notes WHERE NoteID = @noteIDParam"

Dim cmdDel As New SqlCommand(strDelete, ConnNoteGrd)

Dim NoteIDParam As New SqlParameter("@noteIDParam", SqlDbType.Int, 4)
NoteIDParam.Value = NoteID
cmdDel.Parameters.Add(NoteIDParam)

ConnNoteGrd.Open()
cmdDel.ExecuteNonQuery()
dgNotes.DataBind()
ConnNoteGrd.Close()

End Sub

Private Sub dgNotes_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dgNotes.PageIndexChanged
dgNotes.EditItemIndex = -1

dgNotes.CurrentPageIndex = e.NewPageIndex
BindData
End Sub

If somebody could take a look, and point me in the right direction, that
would be great.
My table name in SQL is called NOTES, witht he following
noteID int 4
ToUser nvarchar 100
Body nvarchar 200
Frm nvarchar 100
Date datetime 8

TIA!!!

Rudy
 
L

Lars-Erik Aabech

Hi!

I'm not entirely sure, (since you didn't provide the line number ;) ), but
it looks like the exception is thrown when you try to rebind the table after
deleting the row. (Might be a dropdownlist in your grid?)

When you call dgNotes.DataBind in the DeleteCommand handler, your data
source is gone. (You're only filling it if the page isn't in a postback
state)

Try calling BindData() instad of dgNotes.DataBind() at the end of the
DeleteCommand handler.

HTH,
Lars-Erik
 
G

Guest

Hi Lars!

This is the line I get therror
Line 119: NoteID = dgNotes.DataKeys(e.Item.ItemIndex)
I'll try what you suggested and let you know.
Thanks!
 
L

Lars-Erik Aabech

Hi again!

Then my answer was incorrect. :)
Have you set the DataKeyField attribute of the datagrid?

Like this:
<asp:DataGrid id="dgNotes" runat="Server" DataKeyField="noteID"
....>...</datagrid>

Lars-Erik
 
G

Guest

Thanks Lars!

I think that was it, I also noticed I never made note ID a KEY. Now I'm
geting a login error, but I think I'll be able to fixthat pretty quicly. If I
forgot to a put a Key in last night, God knows what else I forgot to do. LOL

Thanks agian for your help!!!!

Rudy
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top