I have an sql table that returns financial year start_date and end_date, so now i want to use that query to dropdownlist of months starting from april until march. And based on the selected month i get the revenue for that month.So the query for calculating financial year only return 2018/04/01 as start_date and 2019/03/31 as end_date, so bare in mind it's not months, just a date .Of course i will be connected to a server and choose my database and table. see what i tried below.
public class Fyear
{
public string FiscalY_Start { get; set; }
public string FiscalY_End { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
//Data Source=cntrra02-sql-rs;Initial Catalog=Training_Database;Integrated Security=True
DateTime start_date;
DateTime end_date;
List<Fyear> list = new List<Fyear>();
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(@"Data Source=serv_name;Initial Catalog=Database_name;Integrated Security=True");
SqlCommand cmd = new SqlCommand(@"SELECT [ST_FI], [END_FY] FROM [FiscalYear]", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
start_date = Convert.ToDateTime(rdr["ST_FI"]);
end_date = Convert.ToDateTime(rdr["END_FY"]);
//DropDownList1.Items.Add(start_date.ToString());
//int count = 0;
while (start_date <= end_date)
{
//count++;
//list.Add(new Fyear{ FiscalY_Start = start_date.ToString("MMMM")});
start_date = start_date.AddMonths(1);
DropDownList1.Items.Add(start_date.ToString());
}
}
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
// DropDownList1.DataSource = dt;
// DropDownList1.DataBind();
}