Solved

Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) Causing errors

Posted on 2004-04-14
9
1,806 Views
Last Modified: 2007-12-19
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="horizontal" >
' Header template removed
' Footer template removed
<ItemTemplate>
  <TR>
    <TD><%# container.dataitem("reported_date") %></TD>
    <TD><asp:Label id=lbllink Visible="True" Runat="server"></asp:Label></TD>
    <TD><%# container.dataitem("reported_type") %></TD>
    <TD><%# container.dataitem("reported_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(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)    Handles Datalist2.ItemDataBound
 
       'This should allow us to format the newly databound Datalist2 (The shouts one)
       If e.Item.ItemType = ListItemType.AlternatingItem 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.InvalidCastException: 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.AlternatingItem 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)
 
0
Comment
Question by:Scampi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10828643
try it as a DataRow instead of a DataRowView...

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
0
 

Author Comment

by:Scampi
ID: 10828775
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("something"), 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.


0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10828792
what are you binding it to ?
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10828799
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 ...
0
 

Author Comment

by:Scampi
ID: 10828836
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.DBDataRecord - I don't know if this is what its supposed to be, but It might be helpful.
0
 

Author Comment

by:Scampi
ID: 10828861
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.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            Dim drv As System.Data.Common.DbDataRecord = 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

0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 250 total points
ID: 10829175
no I meant I should have ooked to see what was getting bound I was assuming a datatable/dataview etc ... in which case it would be a datarow ... I dont work with recordsets ... I dont know what they pass as.

In THIS case it is that ... it depends what you bind ...
0
 
LVL 10

Accepted Solution

by:
EBatista earned 250 total points
ID: 10829320
just to clarify:
DataRowView represent a row of a DataView object, so you cant convert a DataItem (actually System.Data.Common.DBDataRecord as you say) to it, that should work when you bind the control to a dataview objec:
System.Data.DataView dview=aDataTable.DefaultView
where aDataTable contains all the data retrieved by the query string you use.

regards
0
 

Author Comment

by:Scampi
ID: 10829722
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 :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question