how to get only years in dropdownlist in c#asp.net

Posted by Raj_Chennai under ASP.NET on 3/6/2012 | Points: 10 | Views : 21518 | Status : [Member] | Replies : 5
how to get only year and bind into dropdownlist dynamically for example 2010,2011........

raj


Responses

Posted by: Alok on: 3/6/2012 [Member] Starter | Points: 25

Up
0
Down
using system.collection.generaics;
dropdownlist.datasource=GetYear();
dropdownlist.databind();

public list string<string>GetYear()
{
list<string>get=new list<string>();
sqlcommand cmd=new sqlcmmand("select datapart(year,[date]) as year from tbl_Name ",con);
con.open();
sqldatareader dr=cmd.executereader();;

while(dr.read())
{
get.add(dr["year"].tosting());

}
con.close();
return get;
}

Thanks & Regards,
Alokssm

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 3/6/2012 [Member] Silver | Points: 25

Up
0
Down
Use for loop to bind the dropdownlist..

int currentYear = DateTime.Now.Year;
for (int i = 2000; i <= currentYear; i++)
{
ddlList.Items.Add(i);
}

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 3/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Raj,

private void Year()

{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
int year = DateTime.Now.Year - 5;
for (int Y = year; Y <= DateTime.Now.Year; Y++)
{
dropYear.Items.Add(new ListItem(Y.ToString(), Y.ToString()));
}
dropYear.SelectedValue = DateTime.Now.Year.ToString();
}


Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ajirahman on: 3/6/2012 [Member] Starter | Points: 25

Up
0
Down

private void Form1_Load(object sender, EventArgs e)
{
for (var i = 0; i < 6; i++)
{
var year = DateTime.Now.AddYears(i).Year;
yearComboBox.Items.Add(year);
}
}



Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/29/2012 [Member] Silver | Points: 25

Up
0
Down
Mark as Answer if it helpful to you,that motivates...

Raj_Chennai, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response