Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Specified cast is not valid.!!!!

Hi,
I got the following error when i was updating a record.

"Server Error in '/UniplusWebClient' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
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.InvalidCastException: Specified cast is not valid.

Source Error:


Line 54:        Dim i As Integer
Line 55:        For i = 0 To UniplusDataSet.EmpTable.Columns.Count() - 1
Line 56:            Dim t As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)
Line 57:            Dim row As DataRow = UniplusDataSet.EmpTable(e.Item.DataSetIndex)
Line 58:            row(UniplusDataSet.EmpTable.Columns(i).Caption) = t.Text


Source File: c:\inetpub\wwwroot\UniplusWebClient\WebForm1.aspx.vb    Line: 56

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
  UniplusWebClient.WebForm1.DataGrid1_UpdateCommand1(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\UniplusWebClient\WebForm1.aspx.vb:56
  System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
  System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
  System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
  System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
  System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
  System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
  System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
  System.Web.UI.Page.ProcessRequestMain() +1292


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032 "

And the code i used is :
"Private Sub DataGrid1_UpdateCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        Dim i As Integer
        For i = 0 To UniplusDataSet.EmpTable.Columns.Count() - 1
            Dim t As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)
            Dim row As DataRow = UniplusDataSet.EmpTable(e.Item.DataSetIndex)
            row(UniplusDataSet.EmpTable.Columns(i).Caption) = t.Text
        Next
        If Me.UniplusDataSet.HasChanges() Then
            Dim ws As New UniplusWebClient.localhost.UniplusService
            ws.Credentials = System.Net.CredentialCache.DefaultCredentials
            Dim diffUniplus As New UniplusWebClient.localhost.UniplusDataSet
            diffUniplus.Merge(Me.UniplusDataSet.GetChanges())
            ws.uniplusUpdates(diffUniplus)
            UniplusDataSet.Merge(diffUniplus)
        End If
        DataGrid1.EditItemIndex = -1
        DataGrid1.DataBind()
    End Sub
End Class

The error is in Line 56. that is:
" Dim t As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)"

Can someone help me find whats wrong in the above code.
Nitu

Nitu singh
Friday, October 29, 2004

wouldn't you want t to be a string?

grover
Friday, October 29, 2004

Can you explain a lil bit what all changes i should be doing inorder to make 't' from textbox to a string.
I would really appretiate your help.

Nitu singh
Friday, October 29, 2004

**  For i = 0 To UniplusDataSet.EmpTable.Columns.Count() - 1
            Dim t As TextBox = CType(e.Item.Cells(i).Controls(0), TextBox)
            Dim row As DataRow = UniplusDataSet.EmpTable(e.Item.DataSetIndex)
            row(UniplusDataSet.EmpTable.Columns(i).Caption) = t.Text
        Next
**
Yes, why convert the control to a textbox, only to retrieve the value of the text box later?

Why don't you do something like:
Dim s as string = e.item.cells(i).tostring
.
.
.
row(UniplusDataSet.EmpTable.Columns(i).Caption = s

SongSing Writer
Saturday, October 30, 2004

Yes, why convert the control to a textbox, only to retrieve the value of the text box later?

Why don't you do something like:
Dim s as string = e.item.cells(i).tostring
.
.
.
row(UniplusDataSet.EmpTable.Columns(i).Caption = s

aman
Saturday, October 30, 2004

Thank you SongSing Writer and Aman. I did what you suggested. but now another error poped up. Now it is :

"Server Error in '/UniplusWebClient' Application.
--------------------------------------------------------------------------------

System.FormatException: Input string was not in a correct format. at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.String.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ToInt32(Object value) at System.Data.Common.Int32Storage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <System.Web.UI.WebControls.TableCell> in EID Column. Expected type is Int32.
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.ArgumentException: System.FormatException: Input string was not in a correct format. at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.String.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ToInt32(Object value) at System.Data.Common.Int32Storage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <System.Web.UI.WebControls.TableCell> in EID Column. Expected type is Int32.

Source Error:


Line 56:            Dim t As String = e.Item.Cells(i).ToString
Line 57:            Dim row As DataRow = UniplusDataSet.EmpTable(e.Item.DataSetIndex)
Line 58:            row(UniplusDataSet.EmpTable.Columns(i).Caption) = t
Line 59:        Next
Line 60:        If Me.UniplusDataSet.HasChanges() Then


Source File: c:\inetpub\wwwroot\UniplusWebClient\WebForm1.aspx.vb    Line: 58

Stack Trace:


[ArgumentException: System.FormatException: Input string was not in a correct format.
  at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
  at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
  at System.Convert.ToInt32(Object value)
  at System.Data.Common.Int32Storage.Set(Int32 record, Object value)
  at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <System.Web.UI.WebControls.TableCell> in EID Column.  Expected type is Int32.]
  System.Data.DataColumn.set_Item(Int32 record, Object value) +98
  System.Data.DataRow.set_Item(DataColumn column, Object value) +280
  System.Data.DataRow.set_Item(String columnName, Object value) +72
  UniplusWebClient.WebForm1.DataGrid1_UpdateCommand1(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\UniplusWebClient\WebForm1.aspx.vb:58
  System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
  System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
  System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
  System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
  System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
  System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
  System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
  System.Web.UI.Page.ProcessRequestMain() +1292


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032 "

And now the error in line 58. Now i dont understand whats wrong there.

I would really appretiate you help. Please someone out ther give me a solution.

Nitu
Monday, November 01, 2004

here is an example of how i do the textbox contents to string var conversion:

Dim strCity As String = CType(e.Item.Cells(6).Controls(0), TextBox).Text

The (6) is just the position in my grid. 

grover
Monday, November 01, 2004

I do
dim t as string
t =  ctype(e.Item.FindControl("Name"),TextBox).text

where "name" Is the ID of the textbox in the datagrid.

This means if you count your rows wrong (or add more to the datagrid) you still get the correct content.

I think if you response.write( e.Item.Cells(i).ToString) you will get something like "System.Web.UI.WebControls.TableCell"

you need to do some kind of Ctype ... to get the textbox, then the contents of the textbox.

Response.write is a great tool if you are getting problems like this.. comment out the line that is giving you trouble and write the problem variable out.

Aidan
Monday, November 01, 2004

I'd expect that e.Item.Cells(i).Controls(0) is not actually a textbox. If you can, check it by printing out

  e.Item.Cells(i).Controls(0).GetType().Name

It should be 'Textbox'. If not, that cast won't work.

Steve Cooper
Friday, November 19, 2004

*  Recent Topics

*  Fog Creek Home