Using DateTime Picker With Null Support with MS Access and ASP.Net
page 1 of 2
Published: 16 Apr 2004
Unedited - Community Contributed
How to use date/time picker to send date/time value to database and how to send NULL value to date/time field in database through ASP.Net using VB.
by Sameer Lal
Average Rating: 
Views (Total / Last 10 Days): 24265/ 37

Adding Date/Time picker to your web form:

Adding Date/Time picker to your web form:


Using date picker for input to Date/Time field always made my applications more user friendly and never bothered me and the users about formatting the date/time. It is very simple to add date picker into any of your web forms or search pages in ASP. Net. The built in calendar control comes out very handy.

Drag and drop a Text box (TextBox1) into your form and you can add either the image of calendar or add text with hyperlink to date picker page next to it.




Now go to html view and change the codes as follows:

    <asp:textbox id="TextBox1" style="Z-INDEX: 105; LEFT: 384px; POSITION: absolute; TOP: 56px" runat="server" Width="80px"></asp:textbox><STRONG>&nbsp;</STRONG><STRONG>&nbsp; <A href="'calendar1.aspx?formname=Form1.TextBox1','calendar_window','width=154,height=188');calendar_window.focus()"><STRONG><IMG style="Z-INDEX: 106; LEFT: 472px; POSITION: absolute; TOP: 56px" src="button_date.gif"></STRONG></A>


Now, create another webform, name it calendar1.aspx  and add the following codes on html page.


Add these codes in the head portion of html page:

            <script runat="server">

    Private Sub Calendar1_SelectionChanged(sender As Object, e As System.EventArgs)

        Dim strjscript as string = "<script language=""javascript"">"

        strjscript = strjscript & "window.opener." & Httpcontext.Current.Request.Querystring("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"

        strjscript = strjscript & "</script" & ">" 'Don't Ask, Tool Bug

        Literal1.text = strjscript

    End Sub


    Private Sub Calendar1_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs)

       If e.Day.Date ="d") Then

       e.Cell.BackColor = System.Drawing.Color.LightGray

       End If

    End Sub





And add the following codes in the body:


      <body leftmargin="0" topmargin="0">

            <form runat="server" ID="Form1">

                  <asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_dayrender"

                        DayNameFormat="FirstLetter" BackColor="#FFFFCC" FirstDayOfWeek="Monday" BorderColor="#FFCC66"

                        ForeColor="#663399" Height="188px" Width="154px" BorderWidth="1px" Font-Size="8pt" Font-Names="Verdana"


                        <TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle>

                        <SelectorStyle BackColor="#FFCC66"></SelectorStyle>

                        <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle>

                        <DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle>

                        <SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle>

                        <TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle>

                        <OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle>


                  <asp:Literal id="Literal1" runat="server"></asp:Literal>




And that is it. You are ready to use your date picker to enter the date value through text box into your date/time field of database.

View Entire Article

User Comments

Title: Fantstic   
Name: Jackson Desai
Date: 2009-12-01 5:51:54 AM
Its a jenuin solution that I want for my Web Appliction .Thanx.
Title: No value   
Name: Raves
Date: 2008-12-23 3:55:53 AM
No value coming after clicking the date and windows is not closing ...
Title: Perfect Matter   
Name: Ajay
Date: 2008-09-11 9:00:32 AM
Is't a very use ful matter for me
Title: requst   
Name: vinod
Date: 2008-06-12 6:05:42 AM
but the popup window is not closing after selecting a value... can u explain how to do that also...
Title: null to date format   
Name: Kris Tali
Date: 2007-02-21 1:27:42 AM
the problem is that you can't put "" and dbnull to date
try my solution
Dim IDateTo
If Not IsDate(String.Format("{0:MMM-dd-yyyy}", Me.TxtDateTo.Text)) Then
IDateTo = DBNull.Value
IDateTo = CDate(String.Format("{0:MMM-dd-yyyy}", Me.TxtDateTo.Text))
End If
Title: Date   
Name: Ak
Date: 2006-12-28 1:08:30 PM
Execellent. I was struggling to make this work.
Title: Perfect Match   
Name: Maulik Dave
Date: 2006-12-03 9:54:49 AM
Perfect Match! Just what I was looking for. Even though an article is very old (2004), I was able to use it without trouble in .net 2.0
Title: 2.0 instructions   
Name: Art
Date: 2006-11-01 5:42:36 PM
I tried this, and it would not work for 2.0

I finally figured it out. renames your controls when it renders the page. It also renames your form. So, when you send the literal Form1.TextBox1, that is not what 2.0 renders in the web browser. In reality, its 'aspnetForm' for Form1 (the form name) and '_ctl0_Main_TextBox1' for TextBox1.

When I altered the javascript in calendar1.SelectionChanged, it worked.
Title: Mr   
Name: Babu ( Eranavoor ),Rajesh,Arun,Pondy(Gopal),Rms,Vijay
Date: 2006-10-25 6:15:44 AM
Title: Calendar failure in master Pages.   
Name: Laserman
Date: 2006-09-29 4:42:47 PM
Hi. We are using C# and had success until we decide to
use a Master Page for all our web pages.

The calendar will come up but then, after clicking the date,
the form1.txtFieldname is said to be null or not an object.
We tried adding the id of the content placeholder
which did not work either. Can you help? Thanks.
Title: Code compatibility   
Name: Vineet Bhandari
Date: 2006-09-01 6:26:50 AM
The code doesn't work with 2.0 . Please give some 2.0 compatible code. Neither it displays the code in the text box nor the window gets closed
Title: nice like mandya sugar   
Name: mandyavenkatesh
Date: 2006-07-27 6:28:01 AM
excellent topic we will except some more issues like this
Title: Miss   
Name: Satya Krishna
Date: 2006-07-26 1:50:29 AM
Thanks for the help. It reached my requirement.
Title: Very Nice   
Name: arijit chatterjee
Date: 2006-07-21 3:45:12 AM
Very Nice Code.
But how to fix the always on top of the calling page ?
Someone asked for same for windows application. he is a fool. It is already there.
Title: workable as standalone programme?   
Name: kelvin
Date: 2006-06-26 10:50:48 PM
can i use this on a windows application instead of a web application. is it still workable?
Title: Ms   
Name: Dhivya
Date: 2006-06-16 5:21:24 AM
Very nice,and working gud
Title: In a FormView   
Name: Bryan
Date: 2006-06-13 11:51:35 AM
Anyone try to make this work inside of a FormView control? I'm having a problem where it fails to close the calendar as well as failing to transfer the selected date back to the textbox. It's all inside of a FormView in the EditItemTemplate.
Title: Excellent Job   
Name: Jignesh Patel
Date: 2006-06-09 12:46:26 PM
Excellent..This is what I really wanted. Thanks a lot.
Title: Cool   
Name: dhruvin
Date: 2006-05-23 2:25:02 PM
keep it up very good.i have no words to expalin
Title: figured out!!   
Name: kid
Date: 2006-05-16 9:42:59 PM
kid found the solution to the problem...
it is to ignore and run the code.. it works!!
Title: help... error!!   
Name: kid
Date: 2006-05-16 1:53:17 AM
i was prompt after writing the script which needed to be place in the head portion of calendar1.aspx

error falls in this area:
strjscript = strjscript & "</script" & ">"
'Don't Ask, Tool Bug...

wat should i do??
Title: Code for 2.0??   
Name: vLo
Date: 2006-03-29 3:51:11 PM
Has anyone make this worked with 2.0?? I used it w/ 1.1 and had no problem. I am not sure why it doesn't close the window, nor bring in the date to the textbox :(

Please provide any tip you have. Thanks!
Title: Thanks Sameer ji   
Name: Omprakash
Date: 2006-01-14 12:57:25 AM
Thanks a lot , this is now easy for me to go
my email
Title: Multiple Date time pickers   
Name: c
Date: 2005-10-25 8:46:48 AM
Has anyone tried to use more than one on a page? I have 3 textboxes 3 images etc.. but only one will return the date to the textbox. I have the correct textbox ids in the code and it seems to be adding the right javascript to the literal control?
Title: sql server   
Name: balaji
Date: 2005-09-27 4:26:03 AM
how it change the code for sql server?
where to put this? in code behind or class function
Title: date wont work   
Name: davetree
Date: 2005-09-16 5:44:45 AM
make sure the textboxes that you have are of the same id as the line 'calendar1.aspx?formname=Form1.Textbox1{change this textbox name to the name of your textbox(s) ie i have 1 as txtfromDate so the line is 'calendar1.aspx?formname=Form1.txtfromDate}
That was how i solved my problem which was the same as you said urs was - hope it helps you
Title: it's not working for me   
Name: Kim
Date: 2005-09-14 11:07:32 AM
The text doesn't display in the textbox
and the calender window doesn't close !!
Please help
Title: For it to work in Firefox... Take 2   
Name: Eric Brousseau
Date: 2005-08-09 5:37:35 PM
Sorry, I made a typo in the script in my last post. There is a dot after the word "document"

strjscript = strjscript & "window.opener.document." & Httpcontext.Current.Request.Querystring("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"

By the way, your idea was realy helpfull. Keep up the good work!
Title: For it to work in Firefox...   
Name: Eric Brousseau
Date: 2005-08-09 5:26:08 PM
This code works well in Internet Explorer, but not in Firefox. If you want it to work, you must modify the JavaScript part from the calendar page like this:

strjscript = strjscript & "window.opener.document" & Httpcontext.Current.Request.Querystring("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"

See, the "document" word is missing from your code and it crashes the FirefoxJavaScript compiler... Seems like IE is able to "understand" it both way.

And also, you should use RegisterStartupScript to add the JavaScript code to the WebForm in place of a Literal.
Title: Great!   
Name: Pancho
Date: 2005-08-03 2:06:19 PM
It works great, thankyou sooooo much, thats what i was looking for :D
Title: not working for me   
Name: kkk
Date: 2005-06-28 4:38:03 AM
I am not able to dispaly selected date value in textbox
Title: Mr   
Name: hari
Date: 2005-06-13 3:12:32 AM
thats great.
thnx a lot for help.
Title: Beautiful   
Name: Developer
Date: 2005-04-21 12:08:02 PM
Works like a charm. Exactly what my project needed. Do you plan to put up a version with date and time??

Title: Sweet   
Name: Mark
Date: 2005-04-06 6:02:07 AM
Excellent. That was exactly what I was looking for, nothing more, nothing less.
Title: Problem in ur Pop Up Calendar   
Name: Baber Saeed
Date: 2005-01-09 10:42:27 AM
When we seconds Time try to select another dayte..It does nothing......unless we don't refresh the page it doesn't work..Any solution to this problem.
Title: Wonderful   
Name: Baber Saeed
Date: 2005-01-09 8:46:17 AM
Excellent..This is what I really wanted. Thanks a lot.
Title: Mr.   
Name: David Teasdale
Date: 2004-11-19 7:31:43 AM
Excellent..Brilliant...Thanks for the help.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-17 8:07:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search