1

I need to collect date and time from ASP.Net page and pass that to MS SQL DateTime Variable. I should use jQuery datepicker here.

So I'm trying to use a jQuery datepicker to collect date and then a couple of dropdowns to record time and then concatenate all the three to pass on to SQL but it doesn't work.

Can someone guide me what is the best process here?

1 Answer 1

1

This is a rough guide, as I handwrote it..

I presume you're using WebForms, but if not it's not that hard to adapt to MVC.

jQuery Datepicker has an option called altField which updates your chosen field whenever the datepicker is changed.

You would usually set this field to be a hidden field in the HTML markup. In ASP.NET there is a server control called a HiddenField that renders a hidden input on the page. So on the client side, you would do something like this:

<!-- asp controls on the page -->
<asp:HiddenField runat="server" ID="dateHdn" ClientIDMode="Static"/>
<asp:TextBox runat="server" ID="dateTxt" ClientIDMode="Static"/>

<script type="text/javascript">
// document.ready shorthand
$(function() {
  $('#dateTxt').datepicker({
    altField: $('#dateHdn'),
    altFormat: "yy-mm-dd"
  });
});
</script>

Then once the page is posted back, retrieve the value from the Hidden Field on the server side. As you can see the date format that jQuery datepicker uses is similiar to the date format that MS SQL uses for datetime (example ANSI SQL format: 2012-06-18 10:34:09) . Just to be sure you should use var date = DateTime.Parse(dateHdn.Value); to ensure that the date is valid DateTime.

For the further requirement of the time, you should use a separate datepicker / hidden field. Then do something like the below to create a new DateTime object out of the first DateTime object you created from parsing the date and create a new TimeSpan object from the values of the time dropdowns (hours, minutes, seconds):

var hour = int.Parse(hour.SelectedValue);
var minute = int.Parse(minute.SelectedValue);
var second = int.Parse(second.SelectedValue); // or 0
TimeSpan ts = new TimeSpan(hour, minute , second); // hour value is 24hr i.e. 23 = 11pm
date = date.Date + ts;

You need to do it this way as DateTime objects are immutable (meaning they can't be changed after they've been created). However you can assign a new value to the reference.

I hope this helps!

n.b. once this is all working, you should use DateTime.TryParse() because you can never be sure that the values in the hidden fields will be parseable, and this will cause an error. Same with int.TryParse()

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Man, for now I'm passing the date as a string to database and it works fine. For time I'm using a couple of drop downs and sending them as integers. Very soon I will need to change that and I'll use your solution then. Really appreciate your time friend. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.