ASP.NET Calendar Control Sample | Using Calendar control


Needed SQL Commands

create table tabJourneyDetails
(
PassName varchar(30),
JourneyDate datetime,
ReturnData datetime
)

create procedure spCreateJourneyDetails 
( 
@pname varchar(30), 
@jdate  datetime, 
@rdate datetime 
) 
as 
begin 
    insert into tabJourneyDetails values(@pname,@jdate,@rdate) 
end

Calendar.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Calendar.aspx.cs" Inherits="WebDemo01.Calendar" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <table style="width: auto;">
            <tr>
                <td colspan="2">
                    <h2>Calendar control</h2>
                </td>
            </tr>
            <tr>
                <td>Enter Passenger Name
                </td>
                <td>
                    <asp:TextBox ID="txtPassName" runat="server"                               Width="205px" BackColor="#FFFF99"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Select Journey Date
                </td>
                <td>
                    <asp:Calendar ID="cldrJourneyDate" runat="server"                                           Width="200px">
                        <OtherMonthDayStyle BackColor="#FFFF66" />
                    </asp:Calendar>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Select Return Date
                </td>
                <td>
                    <asp:Calendar ID="cldrReturnDate" runat="server"                                                 Width="200px">
                        <OtherMonthDayStyle BackColor="#FFFF66" />
                    </asp:Calendar>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server"                                   Text="Submit" Width="100px" 
                     OnClick="btnSubmit_Click" 
                     BackColor="#339933" Font-Bold="True" 
                     ForeColor="White" />&nbsp;
                    <asp:Button ID="btnNext" runat="server" 
                    Text="Next" Width="98px" 
                    OnClick="btnNext_Click" 
                    BackColor="#339933" Font-Bold="True" 
                    ForeColor="White" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblMsg" runat="server" 
                      Text="Hello user.." Font-Bold="True">
                    </asp:Label>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Calendar.aspx.cs
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace WebDemo01
{
    public partial class Calendar : System.Web.UI.Page
    {
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Step1 (Connection estab)
            string conStr = ConfigurationManager
            .ConnectionStrings["SampleDBConStr"].ConnectionString;

            SqlConnection con = new SqlConnection(conStr);
            con.Open();

            //step2 (Initialize command object with a query)
            SqlCommand cmd =
            new SqlCommand("spCreateJourneyDetails", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@pname", txtPassName.Text);
            cmd.Parameters.AddWithValue("@jdate", cldrJourneyDate
                                  .SelectedDate.ToShortDateString());
            cmd.Parameters.AddWithValue("@rdate", cldrReturnDate
                                  .SelectedDate.ToShortDateString());

            //step3 -(Execute command object)
            cmd.ExecuteNonQuery();

            //step4 - send ack
            lblMsg.Text = txtPassName.Text + 
                                    "'s journey details Saved....";
            cmd.Dispose();
            con.Close();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            txtPassName.Text = "";
            cldrReturnDate.SelectedDate = DateTime.Now;
            cldrJourneyDate.SelectedDate = DateTime.Now;
            lblMsg.Text = "Enter new/next journey details....";
            txtPassName.Focus();
        }
    }
}

No comments:

Post a Comment