I want to display all e-mail addresses from my database in a TextBox. The reason I want to use a TextBox is that I want them in a single row, separated by "; " and that's not possible in a ListBox (right?) which I was using from the beginning.

So - I want to change my "display in ListBox"-code to "display in TextBox"-code. It doesn't seem to be enough just to change ListBox.Items.Add to TextBox.Text (as shown in code below), so what do I have to do to make it work (collect all addresses in an array first and then from there to the TextBox is a thought I'm having but maybe there's a better way)? The TextBox Mode is set to MultiLine.

SqlConnection conn = new SqlConnection(config);
       conn.Open();

       string sql = "SELECT email FROM NewslttrRep";

       SqlCommand comm = new SqlCommand(sql, conn);
       SqlDataReader dr = comm.ExecuteReader();

       while (dr.Read())
       {
           TextBoxEmailNewsletter.Text = (dr[0] + "; "); //only the last e-mail address in the database table is displayed
       }

       dr.Close();
       conn.Close();

well im not sure what you want to do. that code right there will only ever add one email adress to the box. becuase you said =. that means it refresses everytime it adds one. whay you want to do is do += that will add a new value to the box.

but you would want to put somthing in like

TextBoxEmailNewsletter.Text = ""

so the user can then empty the box. like make a button with that code or somthing and call it reset.

now lets change all that code :P. go back to your database (if you use MS its real simpy you make a qurrie and then make selected the whole table then go to sql view to get the code) and then use the SQL code to select the whole table. then fill that into a dataTable.

myConnect2 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Jordan\\Documents\\Star Sonata\\StarSonata Calculator\\Items.mdb;Persist Security Info=False");

            myConnect2.Open();
            OleDbDataAdapter terradata = new OleDbDataAdapter("SELECT t_firstData.* FROM t_firstData;", myConnect2);
            terradata.Fill(fill);
            myConnect2.Close();

that is code to connect to a MS databse. that is now making a conection to the database then fill a dataset with the table you selected. (dont forget to make the dataSet. DataSet fill = new DataSet();)

from there you can do one of 2 things. you can select a row manualy or do it from the users choise.

a manul selection would look somthing like this

//Convert it to String (not needed but i do it anyway)
//cadata = the Dataset
//Tables means what table in the dataset
//select means what colum you want to select
//("id = 5") means you are selecting the row where the colum id = 5
//[0] means you want to look at that row you just selected. you only selected one row so you know you want to select 0 which means first row
//["email"] this means you want to select the colum addonholder and thake that value
string email = Convert.ToString(cadata.Tables[0].Select("id = 5")[0]["email"])

once you do that you can just do somthing like

textbox1.Text += email + ";";

thats a manul selection.

now if you wanted to do somthing liek a user selection then you can do somthing like filling a combox from a databse and putting the value of it to the ID of the email

so lets say you filled a combox from the table with the emails so the combox diplayed all the emails in that combox.

you could do that by somthing like this. im not going to do all the code it just a snipit. this code would go into the combox. but you would need to fill the combox from the databse on load for this to work. ill just paste some of my code that fills combos on load so you can see it at the end.

string email = Convert.ToString(cadata.Tables[0].Select("id = " + combobox1.SelectedValue)[0]["email"]);

textbox1 += email + ";";

that should be pretty easy to follow. basicly what it is doing it now looking at the id of the email selected in the drop down and seeing what id in the table = it. then it looks at the email colum gets the email andd puts it into the string email. Finally it puts the email string into the textbox.

here is how you can fill comoboxs from the data base.

private OleDbConnection myConnect2;

        public AdvancedCa()
        {
            InitializeComponent();
            myConnect2 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\databases\\CA_Data.mdb;Persist Security Info=False");


            myConnect2.Open();
            OleDbDataAdapter terradata = new OleDbDataAdapter("SELECT t_terraforming.* FROM t_terraforming;", myConnect2);
            terradata.Fill(terra);
            myConnect2.Close();


            combobox1.DataSource = terra.Tables[0];
            combobox1.DisplayMember = "email";
            combobox1.ValueMember = "id";

that should work when filling it.

anyway i hope this helps you

Kind Regards,
Jordan

ahh ok :)

yeah i wasnt sure excatly what you where looking for. so i decided i would cover all areas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.