[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

Why am I getting 'There is no row at position 1'?

My dataGrid was working fine until I slightly changed it.  Now I am getting the following error when I click the Delete or Download links.


Server Error in '/Copy_of_Test' Application.
--------------------------------------------------------------------------------

There is no row at position 1.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: There is no row at position 1.

Source Error:


Line 401:        Dim filePath As String = e.Item.Cells(3).Text               ' Replace n with Column Number of you file_path
Line 402:        System.IO.File.Delete(filePath)
Line 403:        FilesDataSet1.Files.Rows.Item(e.Item.ItemIndex).Delete()
Line 404:        filesAdapter.Update(FilesDataSet1)
Line 405:        filesGrid.DataSource = FilesDataSet1.Files.DefaultView
 

Source File: C:\Inetpub\wwwroot\Copy_of_Test\Main_info.aspx.vb    Line: 403

Stack Trace:


[IndexOutOfRangeException: There is no row at position 1.]
   System.Data.DataRowCollection.get_Item(Int32 index)
   Test._Class.filesGrid_DeleteCommand(Object source, DataGridCommandEventArgs e) in C:\Inetpub\wwwroot\Copy_of_Test\Main_info.aspx.vb:403
   System.Web.UI.WebControls.DataGrid.OnDeleteCommand(DataGridCommandEventArgs e)
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() +1292

 
0
jettman26
Asked:
jettman26
1 Solution
 
b1xml2Commented:
Is FilesDataSet1 cached anywhere, like in the ViewState, Session or Cache object? Otherwise, there will be no data available for you to delete the data.

 
0
 
lluthienCommented:
the error indicates that the there is no row 1,
that means,

FilesDataSet1.Files has no rows or only row 0.

what exactly did you change?
0
 
YZlatCommented:
if you want to delete a DtaGrid row, use:

Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
        Dim strSQL As String = "DELETE FROM Table WHERE Field_ID='" & key & "'"

and then run the strSQL query
0
 
jettman26Author Commented:
As far as the Sub filesGrid_DeleteCommand, I changed nothing.  I did change the page_load and also added Uploading capability which I modify the dataGrid in.

The page_Load was:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If User.Identity.IsAuthenticated Then
            lblIdentity.Text = ("The current user is " + User.Identity.Name)
        Else
            lblIdentity.Text = "The current user is not authenticated."
        End If


        If Not IsPostBack Then
            If Not (Request.QueryString("info_id") Is Nothing) Then
                DropDownPopulate()
                GetInfo(Request.QueryString("info_id").ToString)
            Else
                calendar.SelectedDate = DateTime.Now
                DropDownPopulate()
            End If
        End If
**************************************************************************************
The page_Load is now:

If Not IsPostBack Then
            If Not (Request.QueryString("info_id") Is Nothing) Then
                filesAdapter.SelectCommand.Parameters("@Param2").Value = Request.QueryString("info_id")
                filesAdapter.Fill(FilesDataSet1)
                DropDownPopulate()
                GetInfo(Request.QueryString("info_id").ToString)
            Else
                'filesAdapter.SelectCommand.Parameters("@Param2").Value = ""
                'filesAdapter2.Fill(FilesDataSet21)
                calendar.SelectedDate = DateTime.Now
                DropDownPopulate()
            End If
        End If

        If Not IsPostBack Then
            filesGrid.DataSource = FilesDataSet1.Files.DefaultView
            filesGrid.DataKeyField = "file_id"
            filesGrid.DataBind()
        End If
*****************************************************************************************
Here is the uploading that I added.

Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
        Dim myConn As New SqlClient.SqlConnection
        Dim myCommand As New SqlClient.SqlCommand
        Dim myAdapter As New SqlClient.SqlDataAdapter
        Dim CS As String

        CS = "Server=DELL;initial catalog=classes;integrated security=true"
        'uid=USER_NAME;pwd=PASSWORD;"
        myConn.ConnectionString = CS
        myCommand.Connection = myConn
        myAdapter.SelectCommand = myCommand
        If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
            Dim dateNow As String = Date.Today
            Dim properDate As String = Replace(dateNow, "/", "-")
            Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
            Dim fileSize As String = File1.PostedFile.ContentLength
            Dim SaveLocation As String = Server.MapPath("Data") & "\" & properDate & "\" & fn
            Dim folderLocation As String = Server.MapPath("Data") & "\" & properDate
            'myCommand.CommandText = "INSERT INTO FILES(file_name, file_size, file_path, upload_date) VALUES ('" & fn & "', '" & fileSize & "', '" & SaveLocation & "', '" & DateTime.Now & "')"
            Try
                If Directory.Exists(folderLocation) Then
                    File1.PostedFile.SaveAs(SaveLocation)
                Else
                    Directory.CreateDirectory(folderLocation)
                    File1.PostedFile.SaveAs(SaveLocation)
                End If
                Response.Write("The file has been uploaded.")
                myConn.Open()
                If Not (Request.QueryString("info_id") Is Nothing) Then
                    Dim strClassName = Request.QueryString("info_id").ToString
                    myCommand.CommandText = "INSERT INTO FILES(info_id, file_name, file_size, file_path, upload_date) VALUES ('" & strClassName & "', '" & fn & "', '" & fileSize & "', '" & SaveLocation & "', '" & DateTime.Now & "')"
                    myCommand.ExecuteNonQuery()
                    lblMessage.Text = "Record Successfully Added"
                    myConn.Close()
                    filesAdapter.SelectCommand.Parameters("@Param2").Value = Request.QueryString("info_id")
                    filesAdapter.Fill(FilesDataSet1)
                    If IsPostBack Then
                        filesGrid.DataSource = FilesDataSet1.Files.DefaultView
                        filesGrid.DataKeyField = "file_id"
                        filesGrid.DataBind()
                    End If
                Else
                    myCommand.CommandText = "INSERT INTO FILES(info_id, file_name, file_size, file_path, upload_date) VALUES (IDENT_CURRENT('INFO'), '" & fn & "', '" & fileSize & "', '" & SaveLocation & "', '" & DateTime.Now & "')"
                    myCommand.ExecuteNonQuery()
                    myConn.Close()
                    GetInfoID()
                    filesAdapter.SelectCommand.Parameters("@Param2").Value = lblInfoID.Text
                    filesAdapter.Fill(FilesDataSet1)
                    If IsPostBack Then
                        filesGrid.DataSource = FilesDataSet1.Files.DefaultView
                        filesGrid.DataKeyField = "file_id"
                        filesGrid.DataBind()
                    End If
                End If

               
            Catch Exc As Exception
                Response.Write("Error: " & Exc.Message)
            End Try
        Else
            Response.Write("Please select a file to upload.")
        End If
    End Sub
*****************************************************************************************
Here is the Delete that I have always had.

Private Sub filesGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles filesGrid.DeleteCommand
        Dim filePath As String = e.Item.Cells(3).Text               ' Replace n with Column Number of you file_path
        System.IO.File.Delete(filePath)
        FilesDataSet1.Files.Rows.Item(e.Item.ItemIndex).Delete()
        filesAdapter.Update(FilesDataSet1)
        filesGrid.DataSource = FilesDataSet1.Files.DefaultView
        filesGrid.EditItemIndex = -1
        filesGrid.DataBind()
    End Sub

0
 
jettman26Author Commented:
I put in this code in the filesGrid_DeleteCommand Sub.
Dim i As Short
        i = FilesDataSet1.Tables(0).Rows.Count()

i=0.  I guess that means that the dataSet is empty.  I don't understand this since the DataGrid is showing the correct records.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now