Results 1 to 2 of 2

Thread: SQL SELECT Statement with ListView

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159

    SQL SELECT Statement with ListView

    Hey im trying to fill a ListView object with data selected from an SQL SELECT command

    but im not sure how to do it

    here is my code i have attempted:

    Code:
            Dim mySqlCommand As sqlcommand = New SqlCommand
            mySqlCommand.CommandType = CommandType.Text
            mySqlCommand.Connection = SqlConnection1
    
            mySqlCommand.CommandType = CommandType.Text
            mySqlCommand.CommandText = "SELECT showID, showTitle, showOwner FROM shows WHERE showOwner LIKE @showOwner;"
            mySqlCommand.Parameters.Add(New SqlParameter("@showOwner", SqlDbType.VarChar))
            mySqlCommand.Parameters("@showOwner").Value = System.Environment.UserName
    
            Try
                mySqlCommand.Connection.Open()
                
    
    
    
            Catch ex As Exception
    
    MessageBox.Show(ex.Message)
    
            End Try
    
    mySqlCommand.Connection.Close()
    im not sure but do u have to declare a datareader then initiate a while loop

    thanks

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: SQL SELECT Statement with ListView

    Yes sir, that's all you needed to add.
    VB Code:
    1. Dim mySqlCommand As sqlcommand = New SqlCommand
    2.         mySqlCommand.CommandType = CommandType.Text
    3.         mySqlCommand.Connection = SqlConnection1
    4.  
    5.         mySqlCommand.CommandType = CommandType.Text
    6.         mySqlCommand.CommandText = "SELECT showID, showTitle, showOwner FROM shows WHERE showOwner LIKE @showOwner;"
    7.         mySqlCommand.Parameters.Add(New SqlParameter("@showOwner", SqlDbType.VarChar))
    8.         mySqlCommand.Parameters("@showOwner").Value = System.Environment.UserName
    9.  
    10. [b]Dim dr As System.Data.SqlClient.SqlDataReader[/b]
    11.         Try
    12.            
    13.             SqlConnection1.Open
    14.          [b]dr = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)  
    15.     While dr.Read
    16.         MyListView.Items.Add(dr(0))
    17.     End While[/b]
    18.      
    19.         Catch ex As Exception
    20.  
    21. MessageBox.Show(ex.Message)
    22.  
    23.         Finally
    24.       [b]dr.Close[/b]
    25.         End Try

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