Scampi
asked on
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) Causing errors
Ive used and played around with code directly of MSDN, and I can't see why I'm getting a "Specified cast is not valid" error in my code (Marked with a '@#@ in the code below.)
My understanding of this isn't great, but I can't see where there is a problem... If someone could point this out to me I can get back to work on my dissertation! : )
.aspx code
--------------------------
<asp:datalist id="Datalist2" RepeatColumns="1" RepeatDirection="horizonta l" >
' Header template removed
' Footer template removed
<ItemTemplate>
<TR>
<TD><%# container.dataitem("report ed_date") %></TD>
<TD><asp:Label id=lbllink Visible="True" Runat="server"></asp:Label ></TD>
<TD><%# container.dataitem("report ed_type") %></TD>
<TD><%# container.dataitem("report ed_status" ) %></TD>
<TD>'Some other stuff that takes too much room an isn't important</TD>
</TR>
</ItemTemplate>
</asp:datalist>
aspx.VB Codebehind
-------------------------- ------
'Databinding
Dim SQL2 As String
Dim dbConn As New DBConnect ' An object that controls my DB connections
Dim dataobj2 As Object ' Object to store the DB recordset
SQL2 = "select * from sgallreported where reported_type = 1" ' This gets us the Shout reports
dataobj2 = dbConn.Bind(SQL2) ' Fills the dataobj with the database info.
Datalist2.DataSource = dataobj2 ' sets the datasource for the Datalist
Page.DataBind() ' Binds the data
'Problem Function
Private Sub Datalist2_ItemDataBound(By Val sender As System.Object, ByVal e As System.Web.UI.WebControls. DataListIt emEventArg s) Handles Datalist2.ItemDataBound
'This should allow us to format the newly databound Datalist2 (The shouts one)
If e.Item.ItemType = ListItemType.AlternatingIt em Or e.Item.ItemType = ListItemType.Item Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) '<-- @#@ THIS IS THE PROBLEM LINE
Dim link As Label = CType(e.Item.FindControl(" lbllink"), Label)
l ink.Text = "no"
End If
Error Details
--------------------
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.InvalidCastExceptio n: Specified cast is not valid.
Source Error:
Line 233: 'This should allow us to format the newly databound Datalist2 (The shouts one)
Line 234: If e.Item.ItemType = ListItemType.AlternatingIt em Or e.Item.ItemType = ListItemType.Item Then
Line 235: Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Line 236: Dim link As Label = CType(e.Item.FindControl(" lbllink"), Label)
My understanding of this isn't great, but I can't see where there is a problem... If someone could point this out to me I can get back to work on my dissertation! : )
.aspx code
--------------------------
<asp:datalist id="Datalist2" RepeatColumns="1" RepeatDirection="horizonta
' Header template removed
' Footer template removed
<ItemTemplate>
<TR>
<TD><%# container.dataitem("report
<TD><asp:Label id=lbllink Visible="True" Runat="server"></asp:Label
<TD><%# container.dataitem("report
<TD><%# container.dataitem("report
<TD>'Some other stuff that takes too much room an isn't important</TD>
</TR>
</ItemTemplate>
</asp:datalist>
aspx.VB Codebehind
--------------------------
'Databinding
Dim SQL2 As String
Dim dbConn As New DBConnect ' An object that controls my DB connections
Dim dataobj2 As Object ' Object to store the DB recordset
SQL2 = "select * from sgallreported where reported_type = 1" ' This gets us the Shout reports
dataobj2 = dbConn.Bind(SQL2) ' Fills the dataobj with the database info.
Datalist2.DataSource = dataobj2 ' sets the datasource for the Datalist
Page.DataBind() ' Binds the data
'Problem Function
Private Sub Datalist2_ItemDataBound(By
'This should allow us to format the newly databound Datalist2 (The shouts one)
If e.Item.ItemType = ListItemType.AlternatingIt
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) '<-- @#@ THIS IS THE PROBLEM LINE
Dim link As Label = CType(e.Item.FindControl("
l ink.Text = "no"
End If
Error Details
--------------------
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.InvalidCastExceptio
Source Error:
Line 233: 'This should allow us to format the newly databound Datalist2 (The shouts one)
Line 234: If e.Item.ItemType = ListItemType.AlternatingIt
Line 235: Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Line 236: Dim link As Label = CType(e.Item.FindControl("
ASKER
Sadly this comes back with the same error...
Is how I'm binding the data to the page causing the problems? I have seen many differnet ways other than
container.dataitem("someth ing"), but I don't know the difference...
But then that doesn't explain why the other Casting
Dim link As Label = CType(e.Item.FindControl(" lbllink"), Label)
works fine.
Is how I'm binding the data to the page causing the problems? I have seen many differnet ways other than
container.dataitem("someth
But then that doesn't explain why the other Casting
Dim link As Label = CType(e.Item.FindControl("
works fine.
what are you binding it to ?
ah a recordset shoulda read first .... i think you would then cast it to a recordset but I have never actually tried using a recordset ...
ASKER
Gregory - So the recordset should be read before it's bound to the DataList? I've had this datalist being populated fine with data without altering the current binding code...
For some more information, the e.Item.DataItem is a System.Data.Common.DBDataR ecord - I don't know if this is what its supposed to be, but It might be helpful.
For some more information, the e.Item.DataItem is a System.Data.Common.DBDataR
ASKER
In fact, sorry Gregory but I've just worked it out...
For public intrest, instead of trying to cast the e.item.dataitem ive used it directly, as so...
If e.Item.ItemType = ListItemType.AlternatingIt em Or e.Item.ItemType = ListItemType.Item Then
Dim drv As System.Data.Common.DbDataR ecord = e.Item.DataItem
Dim link As Label = CType(e.Item.FindControl(" lbllink"), Label)
If drv("reported_status") = "Deleted" Then
link.Text = "no"
End If
End If
For public intrest, instead of trying to cast the e.item.dataitem ive used it directly, as so...
If e.Item.ItemType = ListItemType.AlternatingIt
Dim drv As System.Data.Common.DbDataR
Dim link As Label = CType(e.Item.FindControl("
If drv("reported_status") = "Deleted" Then
link.Text = "no"
End If
End If
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help Greg - your prompting led me to a solution, and thanks to EBatista for the reason why it didn't work. As you see I've called it a draw and split the points. I must be going soft :)
DataRowView Class
Requirements
Namespace: System.Data
Represents a customized view of a DataRow exposed as a fully featured Windows Forms control.
For a list of all members of this type, see DataRowView Members.
System.Object