this is a long thread/question but maybe you'll find this solution of mine helpful, as you can see i use parameters here and this is a dynamic column =)
protected void BindDate()
{
StringBuilder SQLtext = new StringBuilder();
SQLtext.AppendLine(" declare @tsql nvarchar(max) ");
SQLtext.AppendLine(" set @tsql= ");
SQLtext.AppendLine(" ' ");
SQLtext.AppendLine(" With ctemp as( ");
SQLtext.AppendLine(" select convert(varchar(10),sysDate,102) sysDate,convert(varchar(10),WeekDate,102) WeekDate,[Month],[Quarter],[Year] ");
SQLtext.AppendLine(" from sysCalendar ");
SQLtext.AppendLine(" where sysdate<=(select max(nominal_date) from ATTENDANCE_AGENT_T) ");
SQLtext.AppendLine(" and sysDate>=dateadd(MONTH,-12,getdate()) ");
SQLtext.AppendLine(" ) ");
SQLtext.AppendLine(" select distinct ' + @mydate + ' as mydate from ctemp order by '+ @mydate + ' desc ");
SQLtext.AppendLine(" ' ");
SQLtext.AppendLine(" exec(@tsql) ");
string constr = ConfigurationManager.ConnectionStrings["CIGNAConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(SQLtext.ToString()))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@mydate", Radio_range.SelectedValue);
cmd.Connection = con;
con.Open();
DropDownList_Date.DataSource = cmd.ExecuteReader();
DropDownList_Date.DataTextField = "mydate";
DropDownList_Date.DataValueField = "mydate";
DropDownList_Date.DataBind();
con.Close();
}
}
}