jettman26
asked on
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.IndexOutOfRangeExce ption: 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(file Path)
Line 403: FilesDataSet1.Files.Rows.I tem(e.Item .ItemIndex ).Delete()
Line 404: filesAdapter.Update(FilesD ataSet1)
Line 405: filesGrid.DataSource = FilesDataSet1.Files.Defaul tView
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.DataRowCollect ion.get_It em(Int32 index)
Test._Class.filesGrid_Dele teCommand( Object source, DataGridCommandEventArgs e) in C:\Inetpub\wwwroot\Copy_of _Test\Main _info.aspx .vb:403
System.Web.UI.WebControls. DataGrid.O nDeleteCom mand(DataG ridCommand EventArgs e)
System.Web.UI.WebControls. DataGrid.O nBubbleEve nt(Object source, EventArgs e)
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
System.Web.UI.WebControls. DataGridIt em.OnBubbl eEvent(Obj ect source, EventArgs e)
System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
System.Web.UI.WebControls. LinkButton .OnCommand (CommandEv entArgs e)
System.Web.UI.WebControls. LinkButton .System.We b.UI.IPost BackEventH andler.Rai sePostBack Event(Stri ng eventArgument)
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
System.Web.UI.Page.Process RequestMai n() +1292
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.IndexOutOfRangeExce
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(file
Line 403: FilesDataSet1.Files.Rows.I
Line 404: filesAdapter.Update(FilesD
Line 405: filesGrid.DataSource = FilesDataSet1.Files.Defaul
Source File: C:\Inetpub\wwwroot\Copy_of
Stack Trace:
[IndexOutOfRangeException:
System.Data.DataRowCollect
Test._Class.filesGrid_Dele
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Dim key As String = DataGrid1.DataKeys(e.Item.
Dim strSQL As String = "DELETE FROM Table WHERE Field_ID='" & key & "'"
and then run the strSQL query
ASKER
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.IsAuthentica ted 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.QueryStrin g("info_id ").ToStrin g)
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 .Parameter s("@Param2 ").Value = Request.QueryString("info_ id")
filesAdapter.Fill(FilesDat aSet1)
DropDownPopulate()
GetInfo(Request.QueryStrin g("info_id ").ToStrin g)
Else
'filesAdapter.SelectComman d.Paramete rs("@Param 2").Value = ""
'filesAdapter2.Fill(FilesD ataSet21)
calendar.SelectedDate = DateTime.Now
DropDownPopulate()
End If
End If
If Not IsPostBack Then
filesGrid.DataSource = FilesDataSet1.Files.Defaul tView
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=PASSWOR D;"
myConn.ConnectionString = CS
myCommand.Connection = myConn
myAdapter.SelectCommand = myCommand
If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLe ngth > 0 Then
Dim dateNow As String = Date.Today
Dim properDate As String = Replace(dateNow, "/", "-")
Dim fn As String = System.IO.Path.GetFileName (File1.Pos tedFile.Fi leName)
Dim fileSize As String = File1.PostedFile.ContentLe ngth
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(folderLoc ation) Then
File1.PostedFile.SaveAs(Sa veLocation )
Else
Directory.CreateDirectory( folderLoca tion)
File1.PostedFile.SaveAs(Sa veLocation )
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").ToStr ing
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 .Parameter s("@Param2 ").Value = Request.QueryString("info_ id")
filesAdapter.Fill(FilesDat aSet1)
If IsPostBack Then
filesGrid.DataSource = FilesDataSet1.Files.Defaul tView
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 .Parameter s("@Param2 ").Value = lblInfoID.Text
filesAdapter.Fill(FilesDat aSet1)
If IsPostBack Then
filesGrid.DataSource = FilesDataSet1.Files.Defaul tView
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(By Val source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) 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(file Path)
FilesDataSet1.Files.Rows.I tem(e.Item .ItemIndex ).Delete()
filesAdapter.Update(FilesD ataSet1)
filesGrid.DataSource = FilesDataSet1.Files.Defaul tView
filesGrid.EditItemIndex = -1
filesGrid.DataBind()
End Sub
The page_Load was:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If User.Identity.IsAuthentica
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
DropDownPopulate()
GetInfo(Request.QueryStrin
Else
calendar.SelectedDate = DateTime.Now
DropDownPopulate()
End If
End If
**************************
The page_Load is now:
If Not IsPostBack Then
If Not (Request.QueryString("info
filesAdapter.SelectCommand
filesAdapter.Fill(FilesDat
DropDownPopulate()
GetInfo(Request.QueryStrin
Else
'filesAdapter.SelectComman
'filesAdapter2.Fill(FilesD
calendar.SelectedDate = DateTime.Now
DropDownPopulate()
End If
End If
If Not IsPostBack Then
filesGrid.DataSource = FilesDataSet1.Files.Defaul
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
'uid=USER_NAME;pwd=PASSWOR
myConn.ConnectionString = CS
myCommand.Connection = myConn
myAdapter.SelectCommand = myCommand
If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLe
Dim dateNow As String = Date.Today
Dim properDate As String = Replace(dateNow, "/", "-")
Dim fn As String = System.IO.Path.GetFileName
Dim fileSize As String = File1.PostedFile.ContentLe
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(folderLoc
File1.PostedFile.SaveAs(Sa
Else
Directory.CreateDirectory(
File1.PostedFile.SaveAs(Sa
End If
Response.Write("The file has been uploaded.")
myConn.Open()
If Not (Request.QueryString("info
Dim strClassName = Request.QueryString("info_
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
filesAdapter.Fill(FilesDat
If IsPostBack Then
filesGrid.DataSource = FilesDataSet1.Files.Defaul
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
filesAdapter.Fill(FilesDat
If IsPostBack Then
filesGrid.DataSource = FilesDataSet1.Files.Defaul
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(By
Dim filePath As String = e.Item.Cells(3).Text ' Replace n with Column Number of you file_path
System.IO.File.Delete(file
FilesDataSet1.Files.Rows.I
filesAdapter.Update(FilesD
filesGrid.DataSource = FilesDataSet1.Files.Defaul
filesGrid.EditItemIndex = -1
filesGrid.DataBind()
End Sub
ASKER
I put in this code in the filesGrid_DeleteCommand Sub.
Dim i As Short
i = FilesDataSet1.Tables(0).Ro ws.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.
Dim i As Short
i = FilesDataSet1.Tables(0).Ro
i=0. I guess that means that the dataSet is empty. I don't understand this since the DataGrid is showing the correct records.