4334738290
4334738290

Reputation: 393

Populating a combo box with database information

I want to populate a combobox with database information, I am storing my information like so:

CREATE TABLE `websites` 
(
    `id` int(11) NOT NULL,
    `name` varchar(125) NOT NULL,
    `xpath` varchar(250) NOT NULL,
    `time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `websites`
--

INSERT INTO `websites` (`id`, `name`, `xpath`, `time`) 
VALUES
    (1, 'Dailymail', '//div[@itemprop=\\\"articleBody\\\"]', '2017-04-30 23:34:49'),
    (2, 'Skynews', '/html/body/div[1]/div[2]/div[1]/div[3]/div', '2017-04-30 23:35:29'),
    (3, 'BBC news', '//*[@id=\\\"page\\\"]/div[2]/div[2]/div/div[1]/div[1]/div[2]', '2017-04-30 23:35:44'),
    (4, 'Yahoo news', '//*[@id=\\\"Col1-0-ContentCanvas\\\"]/article', '2017-04-30 23:35:57'),
    (5, 'CNBC', '//*[@id=\\\"article_body\\\"]', '2017-04-30 23:36:10'),
    (6, 'CNN', '//*[@id=\\\"body-text\\\"]', '2017-04-30 23:36:27'),
    (7, 'Fox news', '//*[@id=\\\"content\\\"]/div[2]/div[3]/article/div/div[3]/div[2]', '2017-04-30 23:36:40');

I want to show the text as "name" from my database for each individual items, when I try the following it just does one index, how can I have separate them?

scon.Open();

scmd = new MySqlCommand();
string SQL = "SELECT * FROM websites";
scmd.Connection = scon;
scmd.CommandText = SQL;

MySqlDataReader rdr = scmd.ExecuteReader();

int count = rdr.FieldCount;

while (rdr.Read())
{
    for (int i = 0; i < count; i++)
    {
        newssite.Text += rdr.GetValue(i);
    }
}

Upvotes: 1

Views: 36

Answers (1)

Sebastian Lerch
Sebastian Lerch

Reputation: 424

Doing

newssite.Text += rdr.GetValue(i) 

is the same as

newssite.Text = newssite.Text + rdr.GetValue(i) 

so you are just appending to the very same element all the time. Do something like this in your while loop. Here I assume, that newssite is your combobox, please be more specific in future questions.

while (rdr.Read())
{
    newssite.Items.Add(rdr["name"].ToString());
}

Upvotes: 2

Related Questions