c# - How to set Drop down List containing Months to previous month -


i have drop down list contains months. have set previous month. getting null reference exception trying in line dropdownlistbm.items.findbyvalue(datetime.now.addmonths(-1).tostring()).selected = true;

given below code.i thinking has if january current month.

  datetime month = convert.todatetime("1/1/2000");                 (int = 0; < 12; i++)                 {                     datetime nextmont = month.addmonths(i);                     listitem list = new listitem();                     list.text = nextmont.tostring("mmmm");                     list.value = nextmont.month.tostring();                     dropdownlistbm.items.add(list);                 } dropdownlistbm.items.findbyvalue(datetime.now.addmonths(-1).tostring()).selected = true; 

this:

dropdownlistbm.items.findbyvalue(datetime.now.addmonths(-1).tostring()).selected = true;

should be: dropdownlistbm.items.findbyvalue(datetime.now.addmonths(-1).month.tostring()).selected = true;


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -