[英]how to Pass data from dropdownlist to database
我想將日期從三個下拉列表傳遞到數據庫,並且遇到了一個問題(System.ArgumentOutOfRangeException:Year,Month和Day參數描述了無法代表的DateTime。)並且我聲明了數據類型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;
}
您正在錯誤地調用DateTime構造函數。
根據MSDN ,存在DateTime構造函數重載,該重載可以包含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)
);
您還應該確保“文本”字段實際上可以解析為整數,否則,如果用戶提供無效值,您仍然會收到異常。
將此代碼插入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);
嘗試改用ASP.NET日歷控件。 這樣可以節省您很多時間。
嘗試“ DateTime.Parse”。
DateTime.Parse
(
purchinvoice_dropdownlist_monthdate.SelectedItem.Text + "/" +
purchinvoice_dropdownlist_daydate.SelectedItem.Text + "/" +
purchinvoice_dropdownlist_yeardate.SelectedItem.Text
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.