C# 如何将数据从dropdownlist传递到数据库
我想将日期从三个下拉列表传递到数据库,我遇到了一个问题(System.ArgumentOutOfRangeException:Year、Month和Day参数描述了一个不可表示的日期时间。)并且我已经声明了数据类型DateTime,请快速回答我 背后的代码是C# 如何将数据从dropdownlist传递到数据库,c#,asp.net,sql-server-2008,datetime,ado.net,C#,Asp.net,Sql Server 2008,Datetime,Ado.net,我想将日期从三个下拉列表传递到数据库,我遇到了一个问题(System.ArgumentOutOfRangeException:Year、Month和Day参数描述了一个不可表示的日期时间。)并且我已经声明了数据类型DateTime,请快速回答我 背后的代码是 pi.p_date = new DateTime(int.Parse(purchinvoice_dropdownlist_daydate.SelectedItem.Text),int.Parse(purchinvoice_dropdownl
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>
尝试改用ASP.NET日历控件。这将为您节省大量时间。您错误地调用了DateTime构造函数 根据,有一个DateTime构造函数重载,可以接受3个参数。 i、 e 您以错误的顺序传递参数 尝试更改您的代码以改用此代码
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)
);
您可能还应该确保文本字段实际上可以解析为整数,否则,如果用户提供无效值,您仍然会收到异常。请尝试“DateTime.Parse”
在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>
您将日期、月份和年份指定给p_日期的位置???@Mohamed Yousri。。您没有为您的p_日期赋值。它为空或为空。您需要赋值dropdownlist值的值是什么?
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)
);
DateTime.Parse
(
purchinvoice_dropdownlist_monthdate.SelectedItem.Text + "/" +
purchinvoice_dropdownlist_daydate.SelectedItem.Text + "/" +
purchinvoice_dropdownlist_yeardate.SelectedItem.Text
);
<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>
string Issue_Date = TextBox3.Text;
DateTime Date = Convert.ToDateTime(Issue_Date);
Console.WriteLine( Date.Year + "" + Date.Month + "" + Date.Day);