Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Error due to Casting in the code!!

Hi,
I got another error while i was trying to update a record.
The error was as following:
"An unhandled exception occurred during the exception on the current web request. Please review the stack trace for more information about the error and where it originated in the code.
System.Invalid Cast Exception: Specified Cast is not valid"

[Note : Here is the piece of code i used for updating. This code i took from Microsoft documentation to learn .Net. This is not my own code]

Code:
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        Dim i As Integer
        For i = 0 To AuthorsData.CMtable.Columns.Count
            Dim t As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)
            Dim row As DataRow = AuthorsData.CMtable(e.Item.DataSetIndex)
            row(AuthorsData.CMtable.Columns(i - 1).Caption) = t.Text
        Next
        If Me.AuthorsData.HasChanges() Then
            Dim ws As New AuthorsWebClient.localhost.AuthorsService
            ws.Credentials = System.Net.CredentialCache.DefaultCredentials
            Dim diffAuthors As New AuthorsWebClient.localhost.Authors1
            diffAuthors.Merge(Me.AuthorsData.GetChanges())
            ws.UpdateAuthors(diffAuthors)
            AuthorsData.Merge(diffAuthors)
        End If
        DataGrid1.EditItemIndex = -1
        DataGrid1.DataBind()
    End Sub
End Class

I hope some could help me.

Nitu.

Nitu singh
Wednesday, October 27, 2004

Maybe it's too early in the morning, but I can't see a Cast in there anywhere. I think you need to look at the stack trace and see exactly which line of code is crapping out.

NetFreak
Wednesday, October 27, 2004

Dim t As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)


Probably getting a control in the cell that is not a textbox.  Might be a literal control.

smallbiz
Wednesday, October 27, 2004

*  Recent Topics

*  Fog Creek Home