Hi, new to the site, thought someone might be able to help. Heres my problem:
I am creating a datareader and then populating it with a stored procedure. I then want to populate a listview with the datareader. My code is below. It works to a point but the trouble is that when you run the app and click the fill button it populates the list view with the datareader(stored procedure) columns but in the wrong place.
My explanation isn't very good I know but if someone can try give me a hand that would be great cheers!
#Region" Imports "
Imports System.Data.SqlClient
#EndRegion
PublicClass RecentCandidateEdits
'Creating connection string, connection object, command object and datareader
Dim ConnString AsString = _
"Data Source=***;Initial Catalog=*****;User ID = ****;Password = *****"
Dim SqlConn As SqlConnection = New SqlConnection(ConnString)
Dim SqlComm As SqlCommand = New SqlCommand("p_***", SqlConn)
Dim dr As SqlDataReader
PrivateSub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click
If btnFill.Text = "&Fill"Then
Try
'Opening connection to database
SqlConn.Open()
'Execute the command object (stored procedure) and assign it to dr
dr = SqlComm.ExecuteReader
'Read the stored procedure
dr.Read()
lvResults.Columns.Add("Ref Num", 70, HorizontalAlignment.Left)
lvResults.Columns.Add("Candidate", 120, HorizontalAlignment.Left)
lvResults.Columns.Add("Last Edit Date", 100, HorizontalAlignment.Left)
'While the stored procedure is getting read in do the following
While dr.Read
'Declare a new listviewitem named listviewitem
Dim ListViewItem As ListViewItem = New ListViewItem
'create a new subitem of listviewitem of the stored procedures (column index value)'s value
ListViewItem.SubItems.Add(dr.GetValue(0).ToString)
ListViewItem.SubItems.Add(dr.GetValue(1).ToString)
ListViewItem.SubItems.Add(dr.GetValue(2).ToString)
'add listviewitem (all of the columns from stored procedure from above) to the listview
lvResults.Items.Add(ListViewItem)
' lvResults.Items.Add("HI")
EndWhile
'Close the database connection
SqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
EndTry
btnFill.Text = "Clear"
Else
lvResults.Items.Clear()
btnFill.Text = "&Fill"
EndIf
EndSub
I am creating a datareader and then populating it with a stored procedure. I then want to populate a listview with the datareader. My code is below. It works to a point but the trouble is that when you run the app and click the fill button it populates the list view with the datareader(stored procedure) columns but in the wrong place.
My explanation isn't very good I know but if someone can try give me a hand that would be great cheers!
#Region" Imports "
Imports System.Data.SqlClient
#EndRegion
PublicClass RecentCandidateEdits
'Creating connection string, connection object, command object and datareader
Dim ConnString AsString = _
"Data Source=***;Initial Catalog=*****;User ID = ****;Password = *****"
Dim SqlConn As SqlConnection = New SqlConnection(ConnString)
Dim SqlComm As SqlCommand = New SqlCommand("p_***", SqlConn)
Dim dr As SqlDataReader
PrivateSub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click
If btnFill.Text = "&Fill"Then
Try
'Opening connection to database
SqlConn.Open()
'Execute the command object (stored procedure) and assign it to dr
dr = SqlComm.ExecuteReader
'Read the stored procedure
dr.Read()
lvResults.Columns.Add("Ref Num", 70, HorizontalAlignment.Left)
lvResults.Columns.Add("Candidate", 120, HorizontalAlignment.Left)
lvResults.Columns.Add("Last Edit Date", 100, HorizontalAlignment.Left)
'While the stored procedure is getting read in do the following
While dr.Read
'Declare a new listviewitem named listviewitem
Dim ListViewItem As ListViewItem = New ListViewItem
'create a new subitem of listviewitem of the stored procedures (column index value)'s value
ListViewItem.SubItems.Add(dr.GetValue(0).ToString)
ListViewItem.SubItems.Add(dr.GetValue(1).ToString)
ListViewItem.SubItems.Add(dr.GetValue(2).ToString)
'add listviewitem (all of the columns from stored procedure from above) to the listview
lvResults.Items.Add(ListViewItem)
' lvResults.Items.Add("HI")
EndWhile
'Close the database connection
SqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
EndTry
btnFill.Text = "Clear"
Else
lvResults.Items.Clear()
btnFill.Text = "&Fill"
EndIf
EndSub