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