Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while i am binding listview display error like "Unable to cast object of type 'System.Decimal' to type 'System.String'."
VB
Imports System.Data.SqlClient
Public Class ListViewData
    Public Sub FillListView(ByRef MyListView As ListView, _
                                                        ByRef myData As SqlDataReader)
        Dim lvwColumn As ColumnHeader
        Dim itmListItem As ListViewItem

        Dim strTest As String

        Dim shtCntr As Short

        MyListView.Clear()
        For shtCntr = 0 To myData.FieldCount() - 1
            lvwColumn = New ColumnHeader()
            lvwColumn.Text = myData.GetName(shtCntr)
            MyListView.Columns.Add(lvwColumn)
        Next

        Do While myData.Read
            itmListItem = New ListViewItem()
            strTest = IIf(myData.IsDBNull(0), "", myData.GetString(0))
            itmListItem.Text = strTest

            For shtCntr = 1 To myData.FieldCount() - 1
                If myData.IsDBNull(shtCntr) Then
                    itmListItem.SubItems.Add("")
                Else
                    itmListItem.SubItems.Add(myData.GetString(shtCntr))
                End If
            Next shtCntr

            MyListView.Items.Add(itmListItem)
        Loop
    End Sub

End Class
Posted
Updated 22-Mar-11 17:20pm
v2

1 solution

The error is self explanatory. The code assigns a decimal to a string variable.

Debug, find the error place. Use the toString() of the decimal value type or Convert.ToString() method before assign the decimal to string.

If you are not intended to use a string then use appropriate variable declaration.

Do you want someone here rewrite your code for a simple bug?
 
Share this answer
 
Comments
samir 2011 22-Mar-11 23:43pm    
i solve this kind of error but in listview it display only number field rather than display all field of database.
Albin Abel 22-Mar-11 23:52pm    
Can you debug and tell me which line gives the error? Then I would able to help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900