I'm using C# 2010 Express and Sql Compact. I have a table named as "Records" and column named as "Names" I want to list that names in a listbox.
I wrote that code but last line is thorws "ExecuteReader: Connection property has not been initialized." exception.
SqlCeConnection Baglan = new SqlCeConnection("Data Source=|DataDirectory|CeoDatabase.sdf;Password=CeoDB;Persist Security Info=True");
Baglan.Open();
SqlCeCommand BarlariAl = new SqlCeCommand("SELECT Names FROM Barlar");
SqlCeDataReader BarlariOku = BarlariAl.ExecuteReader();
As to what to write next, assuming there's a list box named listbox
(bold assumption given your variable names), you'd write:
while(BarlariOku.Read())
listbox.Items.Add(BarlariOku["Names"]);
As an aside, you're not disposing your objects properly. It should look like this:
using(var conn = new SqlCeConnection("Data Source=|DataDirectory|CeoDatabase.sdf;Password=CeoDB;Persist Security Info=True"))
{
conn.Open();
var comm = new SqlCeCommand("SELECT Names FROM Barlar", conn);
SqlCeDataReader reader = comm.ExecuteReader();
while(reader.Read())
listbox.Items.Add(reader["Names"]);
}