如何将数据从下拉列表传递到数据库

时间:2015-03-23 03:56:29

标签: c# asp.net sql-server-2008 datetime ado.net

我想将日期从三个下拉列表传递到数据库,我遇到了一个问题(System.ArgumentOutOfRangeException:Year,Month和Day参数描述了一个不可表示的DateTime。)并且我已经声明了数据类型DateTime please快点回答我???

背后的代码是

pi.p_date = new DateTime(int.Parse(purchinvoice_dropdownlist_daydate.SelectedItem.Text),int.Parse(purchinvoice_dropdownlist_monthdate.SelectedItem.Text) , int.Parse(purchinvoice_dropdownlist_yeardate.SelectedItem.Text));

和from是:

 <asp:DropDownList ID="purchinvoice_dropdownlist_daydate" CssClass="dropdownliststyle" runat="server">
                <asp:ListItem>Day</asp:ListItem>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
                <asp:ListItem>5</asp:ListItem>
                <asp:ListItem>6</asp:ListItem>
                <asp:ListItem>7</asp:ListItem>
                <asp:ListItem>8</asp:ListItem>
                <asp:ListItem>9</asp:ListItem>
                <asp:ListItem>10</asp:ListItem>
                <asp:ListItem>11</asp:ListItem>
                <asp:ListItem>12</asp:ListItem>
                 <asp:ListItem>13</asp:ListItem>
                <asp:ListItem>14</asp:ListItem>
                <asp:ListItem>15</asp:ListItem>
                <asp:ListItem>16</asp:ListItem>
                <asp:ListItem>17</asp:ListItem>
                <asp:ListItem>18</asp:ListItem>
                <asp:ListItem>19</asp:ListItem>
                <asp:ListItem>20</asp:ListItem>
                <asp:ListItem>21</asp:ListItem>
                <asp:ListItem>22</asp:ListItem>
                <asp:ListItem>23</asp:ListItem>
                <asp:ListItem>24</asp:ListItem>
                <asp:ListItem>25</asp:ListItem>
                <asp:ListItem>26</asp:ListItem>
                <asp:ListItem>27</asp:ListItem>
                <asp:ListItem>28</asp:ListItem>
                <asp:ListItem>29</asp:ListItem>
                <asp:ListItem>30</asp:ListItem>
                <asp:ListItem>31</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="purchinvoice_dropdownlist_monthdate" runat="server" CssClass="dropdownliststyle">
                <asp:ListItem>month</asp:ListItem>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
                <asp:ListItem>5</asp:ListItem>
                <asp:ListItem>6</asp:ListItem>
                <asp:ListItem>7</asp:ListItem>
                <asp:ListItem>8</asp:ListItem>
                <asp:ListItem>9</asp:ListItem>
                <asp:ListItem>10</asp:ListItem>
                <asp:ListItem>11</asp:ListItem>
                <asp:ListItem>12</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="purchinvoice_dropdownlist_yeardate" runat="server" CssClass="dropdownliststyle">
                <asp:ListItem>Year</asp:ListItem>
                <asp:ListItem>2010</asp:ListItem>
                <asp:ListItem>2011</asp:ListItem>
                <asp:ListItem>2012</asp:ListItem>
                <asp:ListItem>2013</asp:ListItem>
                <asp:ListItem>2014</asp:ListItem>
                <asp:ListItem>2015</asp:ListItem>
                <asp:ListItem>2016</asp:ListItem>
                <asp:ListItem>2017</asp:ListItem>
                <asp:ListItem>2018</asp:ListItem>
                <asp:ListItem>2019</asp:ListItem>
                <asp:ListItem>2020</asp:ListItem>
                <asp:ListItem>2021</asp:ListItem>
                <asp:ListItem>2022</asp:ListItem>
                <asp:ListItem>2023</asp:ListItem>
                <asp:ListItem>2024</asp:ListItem>
                <asp:ListItem>2025</asp:ListItem>
            </asp:DropDownList>

,班级是:

  public class purchinvoice
{
    public string purch_serial_number;
    public string sup_name;
    public DateTime p_date;

    public purchinvoice()
    {
        purch_serial_number = null;
        sup_name = null;
        p_date = new DateTime();
    }
    public bool add_purchinvoice(out string msg)
    {
        msg = "";
        bool b = true;
        SqlConnection con = new SqlConnection(DBconnection.connectstr);
        try
        {
            con.Open();
            SqlCommand com = new SqlCommand("add_purch_invoice", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add("@purch_serial_number", SqlDbType.NVarChar).Value = this.purch_serial_number;
            com.Parameters.Add("@sup_name", SqlDbType.NVarChar).Value = this.sup_name;
            com.Parameters.Add("@p_date", SqlDbType.DateTime).Value = this.p_date;
            com.ExecuteNonQuery();
            con.Close();
            b = true;
        }
        catch (Exception ex)
        {
            b = false;
            msg = ex.Message;
            con.Close();

        }
        return b;
    }

4 个答案:

答案 0 :(得分:1)

您正在错误地调用DateTime构造函数。

根据MSDN,有一个DateTime Constructor重载可以带3个参数。 即。

public DateTime(int year, int month, int day) 

您正在以错误的顺序传递参数。

尝试更改后面的代码以改为使用它。

pi.p_date = new DateTime(
    int.Parse(purchinvoice_dropdownlist_yeardate.SelectedItem.Text),        
    int.Parse(purchinvoice_dropdownlist_monthdate.SelectedItem.Text),
    int.Parse(purchinvoice_dropdownlist_daydate.SelectedItem.Text) 
);

您还应该确保文本字段实际上可以解析为整数,否则如果用户提供无效值,您仍会收到异常。

答案 1 :(得分:1)

在html页面中插入此代码:

$string = "00:00:11";
$time   = explode(":", $string);

$hour   = $time[0] * 60 * 60 * 1000;
$minute = $time[1] * 60 * 1000;
$sec    = $time[2] * 1000;

$result = $hour + $minute + $sec;

echo $result;

并在C#中调用此代码中的值:

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />

    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script type="text/javascript">

        $(document).ready(function () {

            $("#TextBox3").datepicker({

                showOn: 'both',
                buttonText: 'Select',
                dateFormat: 'yy/mm/dd',
                chnageMonth: true,
                changeYear: true,

            });

        });

    </script>

答案 2 :(得分:0)

尝试使用ASP.NET Calendar控件。它会为你节省很多时间。

答案 3 :(得分:0)

尝试&#34; DateTime.Parse&#34;。

    DateTime.Parse
    (
    purchinvoice_dropdownlist_monthdate.SelectedItem.Text + "/" + 
    purchinvoice_dropdownlist_daydate.SelectedItem.Text + "/" + 
    purchinvoice_dropdownlist_yeardate.SelectedItem.Text
    );
相关问题