• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

Specified cast is not valid.

I'm getting the error:
Specified cast is not valid.

on this line:
droplist.Items.FindByValue(CType(e.Item.DataItem, System.Data.Common.DbDataRecord).GetInt32(2).ToString()).Selected = True

ideas?
0
net_susan
Asked:
net_susan
  • 6
  • 4
  • 2
5 Solutions
 
aki4uCommented:
you can't cast from DbDataRecord to DataItem.

can you explain what are you trying to do and where it's throwing an error?
0
 
aki4uCommented:
try this:

droplist.Items.FindByValue(CType(System.Data.Common.DbDataRecord, e.Item.DataItem).GetInt32(2).ToString()).Selected = True
0
 
appariCommented:

your syntax is correct. the problem is i think dataitem is not of the type  System.Data.Common.DbDataRecord.

can you post your code where you sre binding data to droplist object.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
net_susanAuthor Commented:
         Private Sub lists ()
               Dim objconn As New SqlConnection("mystring")
               objconn.Open()
               try
                     
               Dim objcmd As New SqlCommand("List2" , objconn)
               objCmd.CommandType = CommandType.StoredProcedure
               objcmd.Parameters.Add("@MemID", SqlDbType.Int).Value = Request.Cookies("sy”)(“CusID")
               Dim dr As SqlDataReader
               dr = objcmd.ExecuteReader()
               dataList1.DataSource = dr
               dataList1.DataBind()
               
               dr.Close
               finally
               objConn.Close()
               objConn.Dispose()
               end try
               
               End Sub

Private Sub datalist_item_bind(ByVal sender As Object, ByVal e As DataListItemEventArgs)
               If ((e.Item.ItemType = ListItemType.Item) or ( e.Item.ItemType = ListItemType.AlternatingItem)) Then
                    Dim droplist As DropDownList
                    droplist = CType(e.Item.FindControl("supplierDrop"), DropDownList)
                    Dim objconn As New SqlConnection("mystring")
                    objconn.Open()
                    try
                    Dim objcmd As New SqlCommand("FoodsEdit", objconn)
                    objCmd.CommandType = CommandType.StoredProcedure
                    Dim dr As SqlDataReader
                    dr = objcmd.ExecuteReader()              
                    droplist.DataSource = dr
                    droplist.DataTextField = "FoodDesc"
                    droplist.DataValueField= "FoodID"
                    droplist.DataBind()
                   
              'Get category id from datalist (the stuff bound to datalist)
              droplist.Items.FindByValue(CType(e.Item.DataItem, System.Data.Common.DbDataRecord).GetInt32(2).ToString()).Selected = True
              dr.Close()
                    finally
                    objConn.Close()
                    objConn.Dispose()
                    end try
             
               End If
          End Sub
0
 
net_susanAuthor Commented:
Aki,

I actually used this before on another page. I don't really understand the code, honestly.

I tried yours, and it said:

BC30691: 'DbDataRecord' is a type in 'Common' and cannot be used as an expression.
0
 
appariCommented:

can you comment the following line  and add the next line and post the result here.

droplist.Items.FindByValue(CType(e.Item.DataItem, System.Data.Common.DbDataRecord).GetInt32(2).ToString()).Selected = True

response.write "Data item type : " + e.item.DataItem.toString
0
 
aki4uCommented:
can you try something like this and tell me the result?

just replace "user_id" with your field name.

Dim user_id As Integer = Int32.Parse(CType(e.Item.DataItem, System.Data.Common.DbDataRecord)("user_id").ToString)
0
 
net_susanAuthor Commented:
I had to do it like this: response.write ("Data item type : " + e.item.DataItem.toString)

It says this (over and over):

System.Data.Common.DbDataRecordData item type :
0
 
net_susanAuthor Commented:
Aki,

That doesn't fail, but it doesn't seem to do anything.
0
 
aki4uCommented:
What do you have in "user_id"?

Can you use response.write(user_id.ToString) and tell me?
0
 
net_susanAuthor Commented:
Hang on, I think the problem may be with my SP.
0
 
net_susanAuthor Commented:
It was in my SP. Yippee, got that part, more to come.

Thanks, this was actually very helpful!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now