Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Index was out of range ERROR <NEWBIE ERROR>

Reply
Thread Tools

Index was out of range ERROR <NEWBIE ERROR>

 
 
=?Utf-8?B?UnVkeQ==?=
Guest
Posts: n/a
 
      07-06-2005
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).BeginIni t()
'
'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.DataGridPageChangedEvent Args) 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
 
Reply With Quote
 
 
 
 
Lars-Erik Aabech
Guest
Posts: n/a
 
      07-06-2005
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

"Rudy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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).BeginIni t()
> '
> '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.DataGridPageChangedEvent Args) 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



 
Reply With Quote
 
 
 
 
=?Utf-8?B?UnVkeQ==?=
Guest
Posts: n/a
 
      07-06-2005
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!

"Lars-Erik Aabech" wrote:

> 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
>
> "Rudy" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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).BeginIni t()
> > '
> > '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.DataGridPageChangedEvent Args) 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

>
>
>

 
Reply With Quote
 
Lars-Erik Aabech
Guest
Posts: n/a
 
      07-06-2005
Hi again!

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

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

Lars-Erik


"Rudy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
>
> "Lars-Erik Aabech" wrote:
>
>> 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
>>
>> "Rudy" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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).BeginIni t()
>> > '
>> > '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.DataGridPageChangedEvent Args) 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

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?UnVkeQ==?=
Guest
Posts: n/a
 
      07-06-2005
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

"Lars-Erik Aabech" wrote:

> Hi again!
>
> Then my answer was incorrect.
> Have you set the DataKeyField attribute of the datagrid?
>
> Like this:
> <aspataGrid id="dgNotes" runat="Server" DataKeyField="noteID"
> ....>...</datagrid>
>
> Lars-Erik
>
>
> "Rudy" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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!
> >
> > "Lars-Erik Aabech" wrote:
> >
> >> 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
> >>
> >> "Rudy" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> > 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).BeginIni t()
> >> > '
> >> > '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.DataGridPageChangedEvent Args) 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
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
Newbie-Index out of range error =?Utf-8?B?UnVkeQ==?= ASP .Net 2 02-03-2005 12:53 AM
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Stefan ASP .Net Datagrid Control 2 07-31-2004 05:52 AM
"Index Was Out of Range" Error Message from the DataGrid Clark Choi ASP .Net 1 01-10-2004 03:32 PM



Advertisments