dcsimg
Results 1 to 12 of 12

Thread: [RESOLVED] VS2015 value of type cannot be converted to string vb net listview item

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,025

    Resolved [RESOLVED] VS2015 value of type cannot be converted to string vb net listview item

    hey
    i am trying to see if a subitems contains the word yes
    and then insert it as a number to the SQL
    but i get this error
    Code:
    value of type 'listviewitem.listviewsubitem cannot be converted to string
    why i see this error?
    this is my code
    Code:
                Dim rsult As String
                
                Dim X As Integer
                For X = 1 To Lsvw.Items.Count - 1
                rsult = Lsvw.Items(X).SubItems(1) ' Here is the error
    
                    If rsult = "YES" Then
                        Cmd.Parameters("@ShowUser").Value = "1"
                    Else
                        Cmd.Parameters("@ShowUser").Value = "0"
                    End If
                    Cmd.ExecuteNonQuery()
                Next
    tnx for any help
    salsa
    Last edited by salsa31; Dec 2nd, 2018 at 06:31 AM.

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: VS2015 value of type cannot be converted to string vb net listview item

    I suggest you must Keep "Option Strict On".
    secondly as the error suggest,, you are trying to store an 'ListViewItem ' into an String Variable, instead you should be using the subitem's Text to get the actual text of that item

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,025

    Re: VS2015 value of type cannot be converted to string vb net listview item

    hey
    i tried like you say but i get this error
    Code:
    failed to convert parameter value from a listviewsubitem to a int32

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,025

    Re: VS2015 value of type cannot be converted to string vb net listview item

    what is this
    Code:
    Option Strict On

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: VS2015 value of type cannot be converted to string vb net listview item

    Quote Originally Posted by salsa31 View Post
    hey
    i tried like you say but i get this error
    Code:
    failed to convert parameter value from a listviewsubitem to a int32
    Then you did it wrong. Maybe show us what you did and then we can determine what's wrong with it.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: VS2015 value of type cannot be converted to string vb net listview item

    Quote Originally Posted by salsa31 View Post
    what is this
    Code:
    Option Strict On
    It's something you can find information on for yourself in seconds.

    https://www.bing.com/search?q=option...gB9YyW&plvar=0

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,025

    Re: VS2015 value of type cannot be converted to string vb net listview item

    Then you did it wrong. Maybe show us what you did and then we can determine what's wrong with it.

    Code:
                Dim rsult As String
                
                Cmd.Parameters.Add("@ShowUser", SqlDbType.Int, 50)
                Dim X As Integer
                For X = 1 To Lsvw.Items.Count - 1
                rsult = Lsvw.Items(X).SubItems(1).Text ' Here is the error
    
                    If rsult = "YES" Then
                        Cmd.Parameters("@ShowUser").Value = "1"
                    Else
                        Cmd.Parameters("@ShowUser").Value = "0"
                    End If
    
                Cmd.ExecuteNonQuery()
                Next

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,025

    Re: VS2015 value of type cannot be converted to string vb net listview item

    well jmcilhinney
    what i am trying to do is
    if the subitem(1) hold the name "YES" then
    store the number (1) as a integer
    else
    store the number (0)

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: VS2015 value of type cannot be converted to string vb net listview item

    I honestly don't see how that line of code could generate that error message. That error message relates to assigning an object of the wrong data type to an command parameter, so it might occur when you set the Value property or when you call ExecuteNonQuery (I'm not sure which). I still don't se how that code could cause it though, as you're not assigning a ListViewSubItem to the Value of a parameter. Something else is going on.

    That said, you do specify the parameter type as Int and then assign a String to it, so that's not really sensible. If the value is supposed to be an Integer then assign an Integer, not a String containing a numeric digit.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: VS2015 value of type cannot be converted to string vb net listview item

    Also, you don't have to if you don't want to but you can make that code far more concise.
    Code:
                Cmd.Parameters.Add("@ShowUser", SqlDbType.Int, 50)
    
                For X = 1 To Lsvw.Items.Count - 1
                    Dim rsult = Lsvw.Items(X).SubItems(1).Text
    
                    Cmd.Parameters("@ShowUser").Value = If(result = "YES", 1, 0)
                    Cmd.ExecuteNonQuery()
                Next
    You could obviously do away with the 'rsult' variable too:
    Code:
                Cmd.Parameters.Add("@ShowUser", SqlDbType.Int, 50)
    
                For X = 1 To Lsvw.Items.Count - 1
                    Cmd.Parameters("@ShowUser").Value = If(Lsvw.Items(X).SubItems(1).Text = "YES", 1, 0)
                    Cmd.ExecuteNonQuery()
                Next
    I would tend to do that unless the expression was very complex or the variable was used in multiple places.

  11. #11

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,025

    Re: VS2015 value of type cannot be converted to string vb net listview item

    Quote Originally Posted by jmcilhinney View Post
    Also, you don't have to if you don't want to but you can make that code far more concise.
    Code:
                Cmd.Parameters.Add("@ShowUser", SqlDbType.Int, 50)
    
                For X = 1 To Lsvw.Items.Count - 1
                    Dim rsult = Lsvw.Items(X).SubItems(1).Text
    
                    Cmd.Parameters("@ShowUser").Value = If(result = "YES", 1, 0)
                    Cmd.ExecuteNonQuery()
                Next
    You could obviously do away with the 'rsult' variable too:
    Code:
                Cmd.Parameters.Add("@ShowUser", SqlDbType.Int, 50)
    
                For X = 1 To Lsvw.Items.Count - 1
                    Cmd.Parameters("@ShowUser").Value = If(Lsvw.Items(X).SubItems(1).Text = "YES", 1, 0)
                    Cmd.ExecuteNonQuery()
                Next
    I would tend to do that unless the expression was very complex or the variable was used in multiple places.
    that is excellent
    thank you very very much

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: VS2015 value of type cannot be converted to string vb net listview item

    As I said, I don't see how changing this:
    Code:
                Dim rsult As String
                
                Dim X As Integer
                For X = 1 To Lsvw.Items.Count - 1
                rsult = Lsvw.Items(X).SubItems(1)
    to this:
    Code:
    Code:
                Dim rsult As String
                
                Cmd.Parameters.Add("@ShowUser", SqlDbType.Int, 50)
                Dim X As Integer
                For X = 1 To Lsvw.Items.Count - 1
                rsult = Lsvw.Items(X).SubItems(1).Text
    could produce this error message:
    failed to convert parameter value from a listviewsubitem to a int32
    As far as I can tell, you'd have had to have changed it to this:
    Code:
                Dim rsult As ListViewItem.ListViewSubItem
                
                Dim X As Integer
                For X = 1 To Lsvw.Items.Count - 1
                rsult = Lsvw.Items(X).SubItems(1)
    or this would do it too:
    Code:
                Dim X As Integer
                For X = 1 To Lsvw.Items.Count - 1
                Dim rsult = Lsvw.Items(X).SubItems(1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width