简体   繁体   中英

How to pass date time to a stored procedure

string _name = txtName.Text;
string _addr = txtAddr.Text;
DateTime _dof = DateTime.Parse(drpDate.SelectedValue + drpMonth.SelectedValue + drpYear.SelectedValue);
string _country = drpCountry.SelectedValue;
string _gender = rdiomf.SelectedValue;

using (SqlConnection conn = conMgr.GetDatabaseConnection())
{
    using (SqlCommand cmd = new SqlCommand("insertStudent",conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@sName", _name);
        cmd.Parameters.AddWithValue("@sAddress", _addr);
        cmd.Parameters.AddWithValue("@sDob", _dof);
        cmd.Parameters.AddWithValue("@sCountry", _country);
        cmd.Parameters.AddWithValue("@sGender", _gender);

        cmd.ExecuteNonQuery();
    }

    conn.Close();
}

The error says error in date. I get date using drop down boxes. How can I fix this? please help me. I'm new to stored procedures and asp.net

为此使用新的日期时间。

DateTime _dof = new DateTime(Convert.ToInt32(drpYear.SelectedValue), Convert.ToInt32(drpMonth.SelectedValue), Convert.ToInt32(drpDate.SelectedValue));

I think it is not a good idea to use three drop downs to construct the date. it is very hard to handle the validation. (eg is Feb 30 2013 a valid date??) Use the .net build in Patetimepicker instead. that is all handled by the framework. If it is client side, use JQuery plugin.

And then you can simply pass that dateTimePicker1.Value to the store procedure parameter.

It will be easier for you.

The way you are doing it you are trying to convert let's say 010213 this is the format you are trying to convert which will fetch String was not recognized as a valid DateTime. error. Use - to differentiate between months and years.

DateTime _dof = Convert.toDateTime(drpDate.SelectedValue +"-"+ drpMonth.SelectedValue +"-"+drpYear.SelectedValue);

OR use this overload.

在此处输入图片说明

you can user jquery datepicker instead of three dropdown..

         <html lang="en">
      <head>
       <meta charset="utf-8" />
           <title>jQuery UI Datepicker - Default functionality</title>
            <link rel="stylesheet"href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
            <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
          <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
            <link rel="stylesheet" href="/resources/demos/style.css" />
                 <script>
                  $(function() {
                  $( "#datepicker" ).datepicker();
                  });
               </script>
           </head>
           <body>
            <p>Date: <input type="text" id="datepicker" /></p>
         </body>
            </html>

You can use your dropdown values as below:-

string _datetime=drpMonth.SelectedValue+drpDate.SelectedValue+drpYear.SelectedValue

Datetime _dof = Convert.ToDateTime(_datetime);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM