Solved

ASP.NET Calendar - add 4 years.

Posted on 2012-03-28
7
494 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

820 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