Solved

ASP.NET Calendar - add 4 years.

Posted on 2012-03-28
7
487 Views
Last Modified: 2012-03-28
I have a page that is used to add records to a Database. I have 2 Calendars - one for the purchase date and one for the retirement date of the equipment, which is 4 years from the purchase date by default, but can be manually changed if required.
So, I have the page and the two calendars, but I can't figure out how to get Calendar2 to Display a date 4 years in the future. Even 4 years from "DateTime.Today" would be fine!
<%@ Import Namespace="System.Data.Oledb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load()
 If Page.IsPostBack = False Then
  Label1.Visible = False
 End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" & _
        "Data Source=E:\Company\New_Intranet\Guestbook2\App_Data\twit.mdb")
    Connection.Open()
        Dim Command As New OleDbCommand("INSERT INTO requests(Username, Forename, Make, Model, Serial, Acquired, Expires, Condition, Location, Comments" & _
    ")VALUES(@Username,@forename,@Make,@model,@Serial,@Acquired,@expires,@condition,@Comments,@location)", Connection)
        Command.Parameters.Add(New OleDbParameter("@Username", TextBox1.Text))
        Command.Parameters.Add(New OleDbParameter("@Forename", TextBox8.Text))
        Command.Parameters.Add(New OleDbParameter("@make", TextBox2.Text))
        Command.Parameters.Add(New OleDbParameter("@model", TextBox3.Text))
        Command.Parameters.Add(New OleDbParameter("@serial", TextBox4.Text))
        Command.Parameters.Add(New OleDbParameter("@acquired", Calendar1.SelectedDate))
        Command.Parameters.Add(New OleDbParameter("@expires", Calendar2.SelectedDate))
        Command.Parameters.Add(New OleDbParameter("@condition", DropDownList1.Text))
        Command.Parameters.Add(New OleDbParameter("@location", DropDownList2.Text))
        Command.Parameters.Add(New OleDbParameter("@comments", TextBox7.Text))
    Command.ExecuteNonQuery()

    Connection.Close()
        Label1.Text = "The Selected Record has been inserted."
    Label1.Visible = True

    'Clear the text boxes after a new insert
    TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id=Head1 runat="server"><link rel="shortcut icon" href="images/headers/favicon.ico">
<title>Form to Add New Hardware</title>
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            color: #FF0000;
        }
        .style2
        {
            width: 275px;
        }
    </style>
</head>
<body>

<form id="form1" runat="server">
<table border="0" cellpadding="3" width="800"><tr><td align="center"><b>Surname</b> 
    <span class=style1>*</span></TD><TD align="center"><asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Please enter a Surname. Enter UNKNOWN if no User assigned"></asp:RequiredFieldValidator></td></tr>
<tr><td align="center"><b>Forename</b>     <span class=style1>*</span></TD><TD align="center"><asp:TextBox ID="TextBox8" runat="server" Text=""></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator7" ControlToValidate="TextBox8" runat="server" ErrorMessage="Please enter a Forename. Enter UNKNOWN if no User assigned"></asp:RequiredFieldValidator></td></tr>

<TR><td align="center"><b>Location</b> <span class="style1">*</span></td><td align="center">
<asp:DropDownList ID="DropDownList2" runat="server">
    <asp:ListItem Selected="True">Bolton (1st)</asp:ListItem>
    <asp:ListItem>Bolton (2nd)</asp:ListItem>
</asp:DropDownList></td></TR>


<tr><td align="center" class="style2"><B>Manufacturer</B> <span class="style1">*</span></TD><TD align="center"> <asp:TextBox ID="TextBox2" runat="server" Text=""></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox2" runat="server" ErrorMessage="Please enter a Manufacturer. Enter UNKNOWN if no Manufacturer available."></asp:RequiredFieldValidator></td></tr>
<TR><td align="center"><B>Model</B> <span class="style1">*</span></td><td align="center"> <asp:TextBox ID="TextBox3" runat="server" Text=""></asp:TextBox>
</td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="TextBox3" runat="server" ErrorMessage="Please enter a Model. Enter UNKNOWN if no Model Number available."></asp:RequiredFieldValidator></td></TR>
<TR><TD align="center"><b>Serial Number</b> <span class="style1">*</span></TD><td align="center"><asp:TextBox ID="TextBox4" runat="server" Text=""></asp:TextBox>
</td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator4" ControlToValidate="TextBox4" runat="server" ErrorMessage="Please enter a Serial Number. Enter UNKNOWN if no Serial Number available."></asp:RequiredFieldValidator></td></TR>
<TR><TD align="center"><b>Acquired Date (d/m/yy)</b> <span class="style1">*</span></TD><td align="center">

    <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
        BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
        DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" 
        ForeColor="#003399" Height="150px" Width="150px" SelectedDate="<%# DateTime.Today %>">
        <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
        <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
        <OtherMonthDayStyle ForeColor="#999999" />
        <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
        <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
        <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" 
            Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
        <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
        <WeekendDayStyle BackColor="#CCCCFF" />
    </asp:Calendar>



</td>

<TR><TD align="center"><b>Retirement Date (d/m/yy)</b> <span class="style1">*</span></TD><td align="center"> 

    <asp:Calendar ID="Calendar2" runat="server" BackColor="White" 
        BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
        DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" 
        ForeColor="#003399" Height="150px" Width="150px">
        <DayHeaderStyle BackColor="#FF6666" ForeColor="Maroon" Height="1px" />
        <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" BackColor="#FF5050" />
        <OtherMonthDayStyle ForeColor="#999999" />
        <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
        <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
        <TitleStyle BackColor="#FF5050" BorderColor="#3366CC" BorderWidth="1px" 
            Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
        <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
        <WeekendDayStyle BackColor="#CCCCFF" />
    </asp:Calendar>


</td>

<TR><td align="center"><b>Condition</b> <span class="style1">*</span></td><td align="center">
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Selected="True">New/Near New</asp:ListItem>
    <asp:ListItem>Good</asp:ListItem>
</asp:DropDownList></td></TR>
<TR><td align="center"><b>Comments</b></td><td align="center"> <asp:TextBox ID="TextBox7" runat="server" Text=""></asp:TextBox></td></TR>
</table>

<asp:Button ID=Button1 runat="server" Text="Insert Record" OnClick="Button1_Click" />

<br />

<asp:Label ID=Label1 runat="server" Text=""></asp:Label>

</form>

</div>
</body>
</html>

Open in new window

The code above has been modified to only include the bits I think are required for this question. If more code is needed, I can supply it.
This kind of question has probably been asked before, but I can't seem to find it. Sorry!
0
Comment
Question by:winstalla
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37776991
Calender1.SelectedDate=DateTime.Today.AddYears(4);
0
 
LVL 35

Expert Comment

by:YZlat
ID: 37777221
I don't think

Calender1.SelectedDate=DateTime.Today.AddYears(4);

on its own will work. Try

Calender1.SelectedDate=DateTime.Today.AddYears(4)
Calender1.VisibleDate=DateTime.Today.AddYears(4)

Open in new window

0
 

Author Comment

by:winstalla
ID: 37777256
Sorry for the daft question, but put the line(s) where, exactly?

Apologies - I'm still fudging(?) my way around ASP.NET. Trying to sprint before I can walk, I think...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 20

Accepted Solution

by:
BuggyCoder earned 350 total points
ID: 37777283
Protected Sub Page_Load()
 If Page.IsPostBack = False Then
  Label1.Visible = False
Calender1.SelectedDate=DateTime.Today.AddYears(4)
Calender1.VisibleDate=DateTime.Today.AddYears(4)
 End If
End Sub

Open in new window

0
 
LVL 16

Expert Comment

by:Rose Babu
ID: 37777300
set visible date with the selected date value.

selected date will only sets the date. but the visible date value will be displayed in browser
Calendar1.SelectedDate = Date.Today

Calendar2.VisibleDate = CDate(Date.Today.AddDays(4))
Calendar2.SelectedDate = CDate(Date.Today.AddDays(4))

Open in new window

Test this
0
 
LVL 16

Expert Comment

by:Rose Babu
ID: 37777321
i delayed to submit. ;-) @BuggyCoder has fixed the issue.
0
 

Author Closing Comment

by:winstalla
ID: 37777324
This is working. Thank you.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now