Mohamed Yousri Mohamed Yousri -4 years ago 128
ASP.NET (C#) Question

how to Pass data from dropdownlist to database

i want to pass the date from a three dropdownlists to database and i have faced a problem that is (System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.) and i have declared datatype DateTime please answer me quickly ???

the code behind is

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


and the from is :

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


and the class is:

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

Answer Source

Try "DateTime.Parse".

    DateTime.Parse
    (
    purchinvoice_dropdownlist_monthdate.SelectedItem.Text + "/" + 
    purchinvoice_dropdownlist_daydate.SelectedItem.Text + "/" + 
    purchinvoice_dropdownlist_yeardate.SelectedItem.Text
    );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download