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?
LVL 1
net_susanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
aki4uCommented:
try this:

droplist.Items.FindByValue(CType(System.Data.Common.DbDataRecord, e.Item.DataItem).GetInt32(2).ToString()).Selected = True
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

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
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.
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
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)
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 :
net_susanAuthor Commented:
Aki,

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

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

Thanks, this was actually very helpful!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.