Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Specified cast is not valid.

Posted on 2006-04-03
12
Medium Priority
?
479 Views
Last Modified: 2010-08-05
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
Comment
Question by:net_susan
  • 6
  • 4
  • 2
12 Comments
 
LVL 10

Expert Comment

by:aki4u
ID: 16366377
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
 
LVL 10

Assisted Solution

by:aki4u
aki4u earned 800 total points
ID: 16366390
try this:

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

Accepted Solution

by:
appari earned 1200 total points
ID: 16366785

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:net_susan
ID: 16367113
         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
 
LVL 1

Author Comment

by:net_susan
ID: 16367117
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
 
LVL 39

Assisted Solution

by:appari
appari earned 1200 total points
ID: 16367284

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
 
LVL 10

Assisted Solution

by:aki4u
aki4u earned 800 total points
ID: 16367346
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
 
LVL 1

Author Comment

by:net_susan
ID: 16370512
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
 
LVL 1

Author Comment

by:net_susan
ID: 16370612
Aki,

That doesn't fail, but it doesn't seem to do anything.
0
 
LVL 10

Assisted Solution

by:aki4u
aki4u earned 800 total points
ID: 16370673
What do you have in "user_id"?

Can you use response.write(user_id.ToString) and tell me?
0
 
LVL 1

Author Comment

by:net_susan
ID: 16370805
Hang on, I think the problem may be with my SP.
0
 
LVL 1

Author Comment

by:net_susan
ID: 16371218
It was in my SP. Yippee, got that part, more to come.

Thanks, this was actually very helpful!
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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

810 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