Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2010, 03:49 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Data type conversion

Hello Everyone,
I am trying out to fetch the Id of the item using ListView but it reports conversion error as Conversion from string "" to type 'Integer' is not valid.
the code snippet is as follows

Code:
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
        con.Open()
        If e.CommandName = "Play" Then
            Dim Id As Integer = ListView1.DataKeys(e.CommandArgument).Values("Id")
End If
End Sub
But the same method for fetching Id with Gridview works fine.
Code:
Protected Sub GridView1_RowCommand1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        con.Open()
If e.CommandName = "play" Then
            Dim Id As Integer = GridView1.DataKeys(e.CommandArgument).Values("Id")
End If
End Sub
I don't know about how to cast it properly.
Any suggestion is appreciated.
Thank You.
 
Old February 7th, 2010, 03:31 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Sophia,

The actual error is saying that it is trying to convert an Id value with an empty string to an integer. If they have clicked a Play button on an item, I would assume that the item would have a proper ID associated with it.
So firstly, check that you set the DataKeyNames property up on the ListView. Otherwise the control will not store the Id as a DataKey.

Secondly, you should probably check for empty IDs anyway, as best practice. A simple check along with an explicit CInt call like this should suffice:

vb Code:
If e.CommandName = "play"
    AndAlso Not String.IsNullOrEmpty(GridView1.DataKeys(e.CommandArgument).Values("Id"))
Then
        Dim Id As Integer = CInt(GridView1.DataKeys(e.CommandArgument).Values("Id"))
End If

HTH
Phil
 
Old February 8th, 2010, 03:11 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi philip,
Thanks for the reply.
I did the same as you mentioned your reply

Code:
If e.CommandName = "play"
    AndAlso Not String.IsNullOrEmpty(ListView1.DataKeys(e.CommandArgument).Values("Id"))
Then
        Dim Id As Integer = CInt(GridView1.DataKeys(e.CommandArgument).Values("Id"))
End If
On debugging I find the same error on the first line but when I delete the items by fetching the Id of items then it works fine. It means Ids are being stored properly.
Code snippet for deleting is

Code:
Protected Sub ListView1_ItemDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewDeleteEventArgs) Handles ListView1.ItemDeleting
        con.Open()
        Dim videoId As Integer = ListView1.DataKeys(e.ItemIndex).Values("Id").ToString()
' Some other code
con.close()
End Sub
Now can you please specify the error in the case of play command.
I've DataKeyNames set to Id in Listview
Thank you once again for reply.
 
Old February 11th, 2010, 12:29 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi, I am still having the problem in the same context, please give some suggestions regarding the topic.
Thanks...
 
Old February 14th, 2010, 03:20 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hello Imar Sir, I am having some problem as mentioned in the first post. Though someone suggested solution but that is not working.
Please try to have a look on the problem.
Thank you...
 
Old February 14th, 2010, 07:36 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi sophia,

As I suggested before, please try to post this in an appropriate forum here at p2p.wrox.com. Your code is not specific to this book; in fact, you're using .NET 3.5 while the book targets 2.0. You'll get better responses if you choose the right forum *and* provide more details about your code, database setup and so on.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
he conversion of a char data type to a datetime data type resulted in an out-of-range adamhw Classic ASP Basics 3 December 23rd, 2009 07:18 AM
Help with data type conversion please androoo Pro VB 6 2 December 24th, 2004 05:45 PM
Help with data type conversion please androoo Pro VB.NET 2002/2003 0 November 29th, 2004 09:06 AM
Data Type Conversion using WHERE IN () Colonel Angus SQL Server 2000 14 August 18th, 2004 08:08 PM
data type conversion Adam H-W SQL Server 2000 2 March 2nd, 2004 08:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.