【问题标题】:how to Pass data from dropdownlist to database如何将数据从下拉列表传递到数据库
【发布时间】:2015-03-23 03:56:28
【问题描述】:

我想将三个下拉列表中的日期传递到数据库,我遇到了一个问题,即(System.ArgumentOutOfRangeException:年、月和日参数描述了无法表示的日期时间。)并且我已经声明了数据类型 DateTime快回复我???

后面的代码是

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));

发件人是:

 <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;
    }

【问题讨论】:

  • 您在哪里为 p_date 分配日、月和年??
  • @Mohamed Yousri .. 您没有为您的 p_date 赋值,它是空的或 null 您需要赋值
  • 下拉列表值的值是多少?

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


【解决方案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) 
);

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

【讨论】:

    【解决方案2】:

    在html页面中插入这段代码:

    <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>
    

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

    string Issue_Date = TextBox3.Text;
    DateTime Date = Convert.ToDateTime(Issue_Date);
    Console.WriteLine( Date.Year + "" + Date.Month + "" + Date.Day);
    

    【讨论】:

      【解决方案3】:

      试试“DateTime.Parse”。

          DateTime.Parse
          (
          purchinvoice_dropdownlist_monthdate.SelectedItem.Text + "/" + 
          purchinvoice_dropdownlist_daydate.SelectedItem.Text + "/" + 
          purchinvoice_dropdownlist_yeardate.SelectedItem.Text
          );
      

      【讨论】:

        【解决方案4】:

        尝试改用 ASP.NET 日历控件。这将为您节省大量时间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-22
          • 1970-01-01
          • 1970-01-01
          • 2021-12-17
          • 1970-01-01
          相关资源
          最近更新 更多