开发者

How to combine month and Year selected from diffrent Dropdowns and save in table in datetime format

开发者 https://www.devze.com 2023-01-31 15:37 出处:网络
..............aspx page code................. <tr> <td> From Date </td> <td> <asp:DropDownList ID=\"drpFromMonth\" runat=\"server\"></asp:DropDownList>

..............aspx page code.................

<tr>
    <td>
        From Date
    </td>
    <td>
        <asp:DropDownList ID="drpFromMonth" runat="server"></asp:DropDownList>
        &nbsp;&nbsp; <asp:DropDownList ID="drpFromYear" runat="server"></asp:DropDownList>
   </td>
   <td>
       To Date
   </td>
   <td>
       <asp:DropDownList ID="drpToMonth" runat="server"></asp:DropDownList>
    &nbsp;&nbsp; <asp:DropDownList ID="drpToYear" runat="server"></asp:DropDownList>
   </td>
</tr>

.........aspx.cs code........

//This is for populate dropdown.

private void PopulateFromMonth() // Populate Month ... e.g. Jan
{
    try
    {
        Generic mGn = GenericBusiness<object>.GetGeneralList("Month");
        drpFromYear.DataSource = mGn.Result;
        drpFromYear.DataTextField = "ListItemName"; // Jan, Feb, etc ....
        drpFromYear.DataValueField = "ListItemValue"; // 1,2,3, etc ....
        drpFromYear.DataBind();
        drpFromYear.Items.Insert(0, GlobalValues.DRPSELECT);
        drpFromYear.Items[0].Value = "0";
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void PopulateToMonth() // Populate Month ... e.g. Jan

{
    try
    {
        Generic mGn = GenericBusiness<object>.GetGeneralList("Month");
        drpFromYear.DataSource = mGn.Result;
        drpFromYear.DataTextField = "ListItemName"; // Jan, Feb, etc ....
        drpFromYear.DataValueField = "ListItemValue"; // 1,2,3, etc ....
        drpFromYear.DataBind();
        drpFromYear.Items.Insert(0, GlobalValues.DRPSELECT);
        drpFromYear.Items[0].Value = "0";
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void PopulateFromYear() // Populate Year ... e.g. 2008, 2009, 2010
{
    try
    {
        Generic mGn = GenericBusiness<object>.GetGeneralList("PMSYear");
        drpFromYear.DataSource = mGn.Result;
        drpFromYear.DataTextField = "ListItemName"; // 2008,2009,2010, etc ....
        drpFromYear.DataValueField = "ListItemValue"; // 8,9,10, etc ....
        drpFromYear.DataBind();
        drpFromYear.Items.Insert(0, GlobalValues.DRPSELECT);
        drpFromYear.Items[0].Value = "0";
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void PopulateToYear() // Populate Year ... e.g. 2008, 2009, 2010
{
    try
    {
        Generic mGn = GenericBusiness<object>.GetGeneralList("PMSYear");
        drpToYear.开发者_如何转开发DataSource = mGn.Result;
        drpToYear.DataTextField = "ListItemName"; // 2008,2009,2010, etc ....
        drpToYear.DataValueField = "ListItemValue"; // 8,9,10, etc ....
        drpToYear.DataBind();
        drpToYear.Items.Insert(0, GlobalValues.DRPSELECT);
        drpToYear.Items[0].Value = "0";
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

public void btnAdd_Click(...) { dr.FromDate = ? dr.ToDate = ? }

Please help me out how it is possible to assign proper date time to dr.FromDate & dr.ToDate


Im going to assume your PopulateToMonth dropdown list was a typo and that it is infact drpFromMonth.

DateTime dateSelected = DateTime.Parse(string.Format("{0}/1/{1}", drpFromMonth.SelectedItem.Value, drpFromYear.SelectedItem.Value));

As always, validate first that your dropdowns actually have a selected value.


I'd use the DateTime constructor that takes year, month and day.

http://msdn.microsoft.com/en-us/library/xcfzdy4x.aspx

Just pass in the year and month values and you can set day to 1 or whatever makes sense to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消