Compiler Error Message: BC30311: Value of type
'System.Web.UI.WebControls.TableCell' cannot be converted to 'Integer'.
on line 63. while trying to write updates using a DataGrids Update method.
Source Error:
Line 61:
Line 62: 'Read in the values of the updated row
Line 63: Dim ID as Integer = e.Item.Cells(0)
Line 64: Dim strTitle as String =
CType(e.Item.Cells(1).Controls(0), TextBox).Text
Line 65: Dim strForeName as String =
CType(e.Item.Cells(2).Controls(0), TextBox).Text
But If I use CType or CInt to convert the TableCell, I get the error...
BC30311: Value of type 'System.Web.UI.WebControls.TableCell' cannot be
converted to 'Integer'. for the line
Dim ID as Integer = CInt(e.Item.Cells(0))
I think I may have a basic conceptual problem with what is happening
here. How does one get a cell from a DataTable in edit mode into an
integer variable?
Here is the code.
Sub dgStaff_Update(sender As Object, e As DataGridCommandEventArgs)
'Read in the values of the updated row
Dim intID as Integer = e.Item.Cells(0)
Dim strTitle as String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Dim strForeName as String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
Dim strSurName as String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text
Dim strRoomNo as String = CType(e.Item.Cells(4).Controls(0),
TextBox).Text
Dim strPhoneNo as String = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
Dim strEmail as String = CType(e.Item.Cells(6).Controls(0), TextBox).Text
Dim strStaff as String = CType(e.Item.Cells(7).Controls(0),
TextBox).Text
Dim strRole as String = CType(e.Item.Cells(8).Controls(0), TextBox).Text
'Construct the SQL statement using Parameters
Dim strSQL as String = _
"UPDATE [user_table] SET [Title] = @Title, " & _
"[ForeName] = @ForeName, [SurName] = @SurName " & _
"[RoomNo] = @RoomNo, [PhoneNo] = @PhoneNo " & _
"[EMail] = @EMail, [Staff] = @Staff " & _
"[Role] = @Role " & _
"WHERE [ID] = @ID"
Const strConnString as String = _
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program Files\Common
Files\ODBC\Data Sources\user_info2.mdb"
Dim objConn as New OleDbConnection(strConnString)
objConn.Open()
Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
' Add Parameters to the SQL query
Dim parameterID as OleDbParameter = _
new OleDbParameter("@ID", OleDbType.Integer)
parameterID.Value = intID
myCommand.Parameters.Add(parameterID)
Dim parameterTitle as OleDbParameter = _
new OleDbParameter("@Title", OleDbType.VarWChar, 75)
parameterTitle.Value = strTitle
myCommand.Parameters.Add(parameterTitle)
Dim parameterForeName as OleDbParameter = _
new OleDbParameter("@ForeName", OleDbType.VarWChar, 75)
parameterForeName.Value = strForeName
myCommand.Parameters.Add(parameterForeName)
Dim parameterSurName as OleDbParameter = _
new OleDbParameter("@SurName", OleDbType.VarWChar, 75)
parameterSurName.Value = strSurName
myCommand.Parameters.Add(parameterSurName)
Dim parameterRoomNo as OleDbParameter = _
new OleDbParameter("@RoomNo", OleDbType.VarWChar, 75)
parameterRoomNo.Value = strRoomNo
myCommand.Parameters.Add(parameterRoomNo)
Dim parameterPhoneNo as OleDbParameter = _
new OleDbParameter("@PhoneNo", OleDbType.VarWChar, 75)
parameterPhoneNo.Value = strPhoneNo
myCommand.Parameters.Add(parameterPhoneNo)
Dim parameterEMail as OleDbParameter = _
new OleDbParameter("@EMail", OleDbType.VarWChar, 75)
parameterEMail.Value = strEMail
myCommand.Parameters.Add(parameterEMail)
Dim parameterStaff as OleDbParameter = _
new OleDbParameter("@Staff", OleDbType.Boolean, 75)
parameterStaff.Value = Convert.ToBoolean(strStaff)
myCommand.Parameters.Add(parameterStaff)
Dim parameterRole as OleDbParameter = _
new OleDbParameter("@Role", OleDbType.VarWChar, 75)
parameterRole.Value = strRole
myCommand.Parameters.Add(parameterRole)
myCommand.ExecuteNonQuery() 'Execute the UPDATE query
objConn.Close() 'Close the connection
'Finally, set the EditItemIndex to -1 and rebind the DataGrid
dgStaff.EditItemIndex = -1
BindData()
End Sub
Mark Fitzpatrick - 08 Aug 2007 14:51 GMT
What are you trying to get at?
Dim ID as Integer = CInt(e.Item.Cells(0)) cannot work!
Remember, that you are accessing a Cells collection. e.Item.Cells(0) simply
gets you a copy of the TableCell control. You can't turn a cell into an
integer. Think of it like this, a table cell renders into a <td></td>.
That's not exactly the behavior here, but you can't turn a non-integer
object into an integer. Can you explain a bit better what you are attempting
to do with this value?
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
> Compiler Error Message: BC30311: Value of type
> 'System.Web.UI.WebControls.TableCell' cannot be converted to 'Integer'. on
[quoted text clipped - 114 lines]
> BindData()
> End Sub
Damien - 08 Aug 2007 14:53 GMT
> Compiler Error Message: BC30311: Value of type
> 'System.Web.UI.WebControls.TableCell' cannot be converted to 'Integer'.
[quoted text clipped - 20 lines]
> here. How does one get a cell from a DataTable in edit mode into an
> integer variable?
e.Item.Cells(0) should be e.Item.Cells(0).Text
the object returned by e.Item.Cells(0) is a TableCell - a rich object
that can have it's font, colour, style, etc read or changed. What
you're wanting to deal with is the *content* of the cell, which is
returned by the Text property.
Damien
DC - 08 Aug 2007 17:11 GMT
> e.Item.Cells(0) should be e.Item.Cells(0).Text
>
[quoted text clipped - 4 lines]
>
> Damien
Great Damien thanks a lot. I think it will take a while for me to get my
head round the objects in .net :D
Luck,