Results 1 to 6 of 6

Thread: help with fuctions [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    help with fuctions [RESOLVED]

    Has this been done before?
    I want to take a datareader and send that to a function along with a listview control as params. I would like the function to take the data in the reader and create a listview item and subitems in the listview parmeter. I thought making it as a function would allow returning true or false easy.

    is this a feasable function? where would i start?

    I figure something like this:

    [vbode]
    public function FillListView(lst as ListView, items as Collection) as boolean

    end function
    [/Highlight]

    that's as far as i got.
    Last edited by Andy; Aug 26th, 2004 at 07:31 AM.

  2. #2
    Member scavenger's Avatar
    Join Date
    Aug 2004
    Location
    Nowhere
    Posts
    42
    Yellow!


    I would like to suggest this code.
    If you want Andy, you can examine it...
    Hope this one helps.... I used this one in my code.
    ahehe...

    VB Code:
    1. Public Function FillListView(ByRef lst As ListView) As Boolean
    2.         Dim SQLComm As New SqlCommand()
    3.         Dim SQLConn As New SqlConnection("<your connection string here>")
    4.         Dim DatReader As SqlDataReader
    5.         Dim i, index As Integer
    6.         index = 0
    7.         SQLComm.CommandText = "<your command text here>"
    8.         SQLConn.Open()
    9.         SQLComm.Connection = SQLConn
    10.         With SQLComm
    11.             .Parameters.Add("<Parameter Name here>")
    12.             'You can add as many parameters as you desire. But it must match the parameters in your commandtext
    13.             DatReader = .ExecuteReader
    14.             While DatReader.Read
    15.                 lst.Items.Add(IsDataNull(DatReader, 0)) 'First Item
    16.                 if DatReader.FieldCount > 0 then
    17.  
    18.                 For i = 1 To DatReader.FieldCount - 1 'If your commandtext has many rows to fetch
    19.                     ' we deducted 1 to the fieldcount becoz it is base-1 not zero-based
    20.                     ' we started at 1 becoz we already fetch the first data
    21.                     lst.Items(index).SubItems(i).Text = IsDataNull(DatReader, i)
    22.                 Next
    23.                 End If
    24.                 index += 1
    25.                 'move on the next fetch
    26.             End While
    27.             DatReader.Close()
    28.         End With
    29.         If lst.Items.Count = 0 Then
    30.             Return False
    31.         Else
    32.             Return True
    33.         End If
    34.     End Function
    35.  
    36.     Private Function IsDataNull(ByVal DataReader As SqlDataReader, ByVal index As Integer) As String
    37.         If IsDBNull(DataReader.GetValue(index)) Then
    38.             Return " "
    39.             'since listviewitems does not allow nulls to be displayed only strings
    40.         Else
    41.             Return DataReader.GetValue(index)
    42.             'just return the original value
    43.         End If
    44.     End Function




    Hope this helps...if not...my apologies....
    Last edited by scavenger; Aug 24th, 2004 at 08:44 PM.
    "It takes great courage to survive"
    -kenshin himura

  3. #3
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    got this also
    VB Code:
    1. Option Strict On
    2.  
    3. Imports System.Data.SqlClient
    4. Public Class Sample
    5.    Public Shared Function fillListView(ByVal lv As ListView, ByVal s As SqlDataReader) As Boolean
    6.       Try
    7.          If Not s.Read Then
    8.             Return False
    9.          End If
    10.          Do
    11.             Dim i As Integer
    12.             Dim li As ListViewItem = lv.Items.Add(s(0).ToString)
    13.             For i = 1 To s.FieldCount - 1
    14.                li.SubItems.Add(s(i).ToString)
    15.             Next
    16.          Loop While s.Read
    17.          s.Close()
    18.          Return True
    19.       Catch ex As Exception
    20.          Return False
    21.       End Try
    22.    End Function
    23. End Class
    sample usage
    VB Code:
    1. Dim cn As New SqlConnection("user id=sa;password=password;initial catalog=northwind")
    2.    Dim da As New SqlDataAdapter()
    3.    Dim dt As New DataTable()
    4.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.       cn.Open()
    6.       da.SelectCommand = New SqlCommand("select * from territories ", cn)
    7.       If (Sample.fillListView(ListView1, da.SelectCommand.ExecuteReader)) Then
    8.          MessageBox.Show("there is")
    9.       Else
    10.          MessageBox.Show("none")
    11.       End If
    12.    End Sub
    i know this isn't efficient enough, hope someone else share their ideas.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Here's what I came up with:
    VB Code:
    1. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Try
    3.             Dim con As New SqlClient.SqlConnection("Initial Catalog=Northwind;Data Source=laptop;user id=sa;Workstation ID=LAPTOP")
    4.             Dim cmd As New SqlClient.SqlCommand("select * from employees", con)
    5.  
    6.             cmd.CommandType = CommandType.Text
    7.             cmd.Connection.Open()
    8.             Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
    9.             FillListView(ListView1, dr)
    10.             dr.Close()
    11.             cmd.Connection.Close()
    12.  
    13.         Catch ex As Exception
    14.             MessageBox.Show(ex.Message)
    15.         End Try
    16.  
    17.     End Sub
    18.     Shared Function FillListView(ByVal lst As ListView, ByVal dr As SqlClient.SqlDataReader) As Boolean
    19.         'Purpose :  Fills  lines of a listview control .
    20.         Try
    21.             Dim col As Integer
    22.             While dr.Read
    23.                 'Create the first item.
    24.                 col = 0
    25.                 lst.Columns.Add(dr.GetName(col), 102, HorizontalAlignment.Left)
    26.                 lst.Items.Add(dr.Item(col).ToString)
    27.                 'create the column names.
    28.                 'don't include binaries or nulls.
    29.                 While col < dr.FieldCount - 1
    30.                     col += 1
    31.                     If Not (dr.Item(col).GetType.ToString = "System.Byte[]") And _
    32.                     Not dr.IsDBNull(col) Then
    33.                         'add the sub items and their column names.
    34.                         lst.Columns.Add(dr.GetName(col), 102, HorizontalAlignment.Left)
    35.                         lst.Items(0).SubItems.Add(CStr(dr.Item(col)))
    36.                     End If
    37.                 End While
    38.             End While
    39.  
    40.  
    41.         Catch ex As Exception
    42.             MessageBox.Show(ex.Message)
    43.         End Try
    44.  
    45.     End Function

    only problem is, the first row ends up with two records, and then that's it. the other rows only show the first item.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    ok, I added an integer variable called 'row' under the 'col' variable and increase that by one after the 'while' that is adding columns and that helps but the data is still off kilter.

    slowly and surely

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    GOT IT!! :D

    Got it all figured out:
    VB Code:
    1. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Try
    3.             Dim con As New SqlClient.SqlConnection("Initial Catalog=Northwind;Data Source=laptop;user id=sa")
    4.             Dim cmd As New SqlClient.SqlCommand("select * from employees", con)
    5.  
    6.             cmd.CommandType = CommandType.Text
    7.             cmd.Connection.Open()
    8.             Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
    9.             FillListView(ListView1, dr)
    10.             dr.Close()
    11.             cmd.Connection.Close()
    12.  
    13.         Catch ex As Exception
    14.             MessageBox.Show(ex.Message)
    15.         End Try
    16.  
    17.     End Sub
    18.     Shared Function FillListView(ByVal lst As ListView, ByVal dr As SqlClient.SqlDataReader) As Boolean
    19.         'Purpose :  Fills  lines of a listview control .
    20.         Try
    21.             Dim col As Integer
    22.             Dim row As Integer
    23.             While dr.Read
    24.                 'Create the first item.
    25.                 col = 0
    26.                 lst.Items.Add(dr.Item(col).ToString)
    27.                 While col < dr.FieldCount - 1
    28.                     'add the sub items and their column names.
    29.                     'filter out anything that may cause an exception.
    30.                     col += 1
    31.                     Select Case True
    32.                         Case dr.Item(col).GetType.ToString = "System.Byte[]"
    33.                             lst.Items.Item(row).SubItems.Add("NA")
    34.                         Case dr.IsDBNull(col)
    35.                             lst.Items.Item(row).SubItems.Add("NA")
    36.                         Case Else
    37.                             lst.Items.Item(row).SubItems.Add(CStr(dr.Item(col)))
    38.                     End Select
    39.                 End While
    40.                 row += 1
    41.             End While
    42.             'add the column headers.
    43.             For x As Integer = 0 To dr.FieldCount - 1
    44.                 lst.Columns.Add(dr.GetName(x), 100, HorizontalAlignment.Left)
    45.             Next
    46.         Catch ex As Exception
    47.             MessageBox.Show(ex.Message)
    48.             Return False
    49.         End Try
    50.  
    51.         Return True
    52.     End Function
    Last edited by Andy; Aug 25th, 2004 at 11:53 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width