How to select distinct year from a datetime column and add the result to a comboBox in C#?
You're not selecting tdate
but you select Year(tdate)
I would modify the query to this:
string sql = "SELECT DISTINCT Year(tdate) AS tdate_year FROM saletransaction ORDER BY Year(tdate) DESC";
and access it with dr["tdate_year"]
You have missed to give the column name in sql query
try this
string sql = "SELECT DISTINCT Year(tdate) AS tdate FROM saletransaction ORDER BY Year(tdate) DESC";
Looks like you have not given an alias to your tdate
query. Therefore when you try and reference tdate
, the column does not exist and Visual Studio throws the error.
Change the query to:
string sql = "SELECT DISTINCT Year(tdate) AS tdate FROM saletransaction ORDER BY Year(tdate) DESC";
Which will return all your results under the column name tdate
.