Mohamed Yousri
Mohamed Yousri

Reputation: 51

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

Upvotes: 0

Views: 126

Answers (4)

zee21
zee21

Reputation: 19

Insert this code in the html page:

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

And call the values from this code in C#:

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

Upvotes: 1

4b0
4b0

Reputation: 22323

Try "DateTime.Parse".

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

Upvotes: 0

failedprogramming
failedprogramming

Reputation: 2522

You are calling the DateTime constructor incorrectly.

According to MSDN, there is a DateTime Constructor overload that can take 3 parameters. i.e.

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

You are passing the parameters in the wrong order.

Try changing your code behind to use this instead.

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

You should also probably make sure that the Text fields can actually be parsed to integers, otherwise you will still receive an exception if the user supplies invalid values.

Upvotes: 1

Jon Tirjan
Jon Tirjan

Reputation: 3694

Try using the ASP.NET Calendar control instead. It will save you a lot of time.

Upvotes: 0

Related Questions