I'm using a sqldatareader to execute a stored procedure. The stored procedure executes successfully, but I want to loop through the results and display them in a multiline textbox. For some reason the while loop goes immediately to the end while. Please let me know if you see what could be wrong with my code. Thanks.
Code:Private Sub FillTableButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillTableButton.Click Try Dim results As New System.Text.StringBuilder Dim ExecuteSprocCommand As New SqlCommand 'server name, stored procedure, stored procedure name ExecuteSprocCommand.Connection = DevDBConnection ExecuteSprocCommand.CommandType = CommandType.StoredProcedure ExecuteSprocCommand.CommandText = "PopulateAssociateTable_proc" ' open the database connection ExecuteSprocCommand.Connection.Open() ' Create an instance of SqlDataReader and execute the stored procedure Dim reader As SqlDataReader = ExecuteSprocCommand.ExecuteReader ' Create a string containing the results to populate the textbox with While reader.Read For i As Integer = 0 To reader.FieldCount - 1 ' separate each field with tab for readability results.Append(reader(i).ToString & vbTab) Next ' new line after each entry so each row is on its own line results.Append(Environment.NewLine) End While ' close the reader and connection since we're in a connected environment reader.Close() ExecuteSprocCommand.Connection.Close() ' write the results to the textbox ResultsTextBox.Text = results.ToString Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub




Reply With Quote