I’m using the code below to grab a list of companies from my database and load them into a list view. It’s working great.
conn.Open();
string pricesqry = "SELECT company, url FROM companies";
SqlCommand pricescmd = new SqlCommand(pricesqry, conn);
SqlDataReader pricesreader = pricescmd.ExecuteReader();
while (pricesreader.Read())
{
ListViewItem company = new ListViewItem(pricesreader["company"].ToString());
company.SubItems.Add(pricesreader["url"].ToString());
company.SubItems.Add("Blank for now..");
pricesList.Items.Add(company);
}
conn.Close();
However, if I wanted to alphabetize my list by company names, by changing my select query to this:
string pricesqry = "SELECT company, url FROM companies ORDER BY company";
No data is loaded into the listview. Remove the order by section and the data appears again. What am I doing wrong?
Credit goes to Minitech.
My ‘Company’ column type was set to Text. Changed to varchar and it’s now working as I expected.