tags:

views:

1483

answers:

4

I have used the Gridview_PageIndexChanging event in asp.net.i have used the code like this:

gridFileDetails.PageIndex = e.NewPageIndex

During the run time when i clicked the next page,it generates an error:

An exception of type 'System.InvalidCastException' occurred in FFK.DLL but was not handled in user code

Additional information: Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.GridViewRow'.

in the RowCommand event,

I have used the following RowCommand event:

Protected Sub gridFileDetails_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridFileDetails.RowCommand

    Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
    Dim rowkey As DataKey = Me.gridFileDetails.DataKeys(row.DataItemIndex)
    Dim key As String = rowkey(0).ToString()
    If e.CommandName = "FileStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("FFKFile.aspx?FileId=" + key)
    ElseIf e.CommandName = "TradexStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("TradeX.aspx?FileId=" + key)
    ElseIf e.CommandName = "BondStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("BondMaster.aspx?FileId=" + key)
    ElseIf e.CommandName = "FDStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("FrmFileDocument.aspx?FileId=" + key)
    ElseIf e.CommandName = "InvoiceStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("InvoiceMaster.aspx?FileId=" + key)
    ElseIf e.CommandName = "PDStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("PackagingDetails.aspx?FileId=" + key)
    End If
End Sub

can you resolve this problem? i am getting the error in the first line in RowCommand event ,ie.,

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

In the PageIndexChanging event i have written as follows:

Protected Sub gridFileDetails_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gridFileDetails.PageIndexChanging gridFileDetails.PageIndex = e.NewPageIndex End Sub

+1  A: 

Instead of this:

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

Use the index from the EventArgs to grab the row programmatically like this:

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gridFileDetails.Rows(index)
Andrew Hare
when i tried ur code,i got the following error:An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user codeAdditional information: Index was out of range. Must be non-negative and less than the size of the collection.
Nandini
This means your grid has no data - check to make sure that the gridview has been databound. Perhaps you are not loading the data on postback and your gridview has viewstate disabled?
Andrew Hare
Please post the stack trace of the exception you are seeing.
Andrew Hare
i am getting this error:An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user codeAdditional information: Index was out of range. Must be non-negative and less than the size of the collection.
Nandini
That is the exception message, I would like to see the stack trace which is the block of text after the message which shows which methods were called prior to the error.
Andrew Hare
+1  A: 

Referencing the MSDN for the System.Web.UI.WebControls.GridViewCommandEventArgs class, the example there for extracting the gridviewrow is this:

  ' Convert the row index stored in the CommandArgument
  ' property to an Integer.
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)

  ' Retrieve the row that contains the button clicked 
  ' by the user from the Rows collection.
  Dim row As GridViewRow = ContactsGridView.Rows(index)

See if that helps you get your row without the cast error.

Jay S
when i tried ur code,i got the following error: An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
Nandini
Have you tried the debugger to see what your objects are instantiated with when this method is run? For example, what is the index that is passed in the e.CommandArgument, and what is the current value of gridFileDetails.Rows?
Jay S
gridFileDetails.Rows.Count returns the value '10'
Nandini
And what is the value of e.CommandArgument? If it is greater than 10, or less than 0, then that would cause the out of range value.
Jay S
Dim index As Integer = Convert.ToInt32(e.CommandArgument)index returns the value '2'
Nandini
So is the out of range exception happening on the MSDN example code? Or is it occurring on another line from your code? The stacktrace of the exception should trace it back to the offending index. The rowKey(0), for example, would fail if rowKey is null or has a length of 0.
Jay S
+1  A: 

This might help :

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gridFileDetails.Rows(index)
Canavar
A: 

Above code gives me error.When i clicked for paging.

ArgumentOutOFRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

pls give me some solution asap.

sonal