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
Re: SQL SELECT Statement with ListView
Yes sir, that's all you needed to add.
VB 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
[b]Dim dr As System.Data.SqlClient.SqlDataReader[/b]
Try
SqlConnection1.Open
[b]dr = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
MyListView.Items.Add(dr(0))
End While[/b]
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
[b]dr.Close[/b]
End Try