0

I just try to read data from database line by line inside list box. Here is my each column section that "reader.GetString" represents - Username, Name, Surname, Department, Faculty etc.. I want them all to appear line by line for every row not side by side with "+=" for example;:

Steve_J

Steve Jackson

Romance-Germanic Department

German language and Literature

and the output of currently written code blocks (all in the same line with spaces between each column) is :

Steve_J Steve Jackson Romance-Germanic faculty Department of German language and Literature

David_21 David Ratchford Faculty of Archaeology Department of Scientific Archeology

How to go about that since we know list box doesn't take "\n"?

Source Code:

private void button2_Click(object sender, EventArgs e)
{
    using (SqlConnection connect = new SqlConnection())
    {
        connect.ConnectionString = @"Data Source=DESKTOP-9I0BNAS\SQLEXPRESS;Initial Catalog=CAVID;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";

        connect.Open();

        using (SqlCommand command = new SqlCommand("select *  from Istifadeciler", connect))
        {
            SqlDataReader reader = command.ExecuteReader();
                     
            while (reader.Read())
            {                        
               string data = reader.GetString(0)+ " ";
               data += reader.GetString(1) + "  ";
               data += reader.GetString(2) + "  ";
               data += reader.GetString(3) + "  ";
               data += reader.GetString(4)+ "  ";
               data += reader.GetString(5) + "  ";
               data += reader.GetInt32(6).ToString()+ "  ";
               data += reader.GetInt32(7).ToString()+ "  ";

               listBox1.Items.Add(data);                        
            }
        }
    }
Community
  • 1
  • 1
  • 1
    Unclear. Where do you add the `"\n"`? `listBox1.Items.Add` will add one `ListItem` for every record. – Tim Schmelter Jul 05 '18 at 11:09
  • what's not working here. I see you're concatentating all your fields and then adding them to your listbox as an item. Is that not what you want? – Charles May Jul 05 '18 at 11:09
  • 1
    Doesn't look like you'd need newlines here. `listBox1.Items.Add(data);` gets executed for every row and should add a seperate entry to the listbox for each. What exactly is the output you're getting and what do you want instead? – Magisch Jul 05 '18 at 11:11
  • Possible duplicate of https://stackoverflow.com/questions/15354533/how-to-put-a-nnew-line-inside-a-list-box – SelvaS Jul 05 '18 at 11:12
  • Try to add all the values in a string list and set the list as `DataSource` to the `ListBox`. Refer this answer https://stackoverflow.com/a/4321309/311255 – SelvaS Jul 05 '18 at 11:16
  • I've just edited the question to be clearer for you – Cavid Hummatov Jul 05 '18 at 11:29
  • @CavidHummatov provide the output as well – kuskmen Jul 05 '18 at 11:35
  • @kuskmen The output is already provided. – Haytam Jul 05 '18 at 11:53
  • Pretty unlikely that ListBox is the correct control to use when you write code like this. Consider ListView with View=Details. – Hans Passant Jul 05 '18 at 13:31
  • @HansPassant how to include it into my code. Could you show as a separate answer ? – Cavid Hummatov Jul 05 '18 at 13:35
  • Those examples already exist, you don't need me to repeat them. https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/listview-control-overview-windows-forms – Hans Passant Jul 05 '18 at 13:38

1 Answers1

0

As suggested by Magisch...

using System.Collections.Generic;
using System.Windows.Forms;

namespace _51189773
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            ListBox lb = new ListBox();
            lb.Items.Add("result 1");
            lb.Items.Add("result 2");
            lb.Items.Add("result 3");
            Controls.Add(lb);
        }
    }
}
blaze_125
  • 2,262
  • 1
  • 9
  • 19