I was hoping someone might give me a pointer on how to validate a calendar control. I have a page where the user selects a date from the calendar and the date is inserted into the database. This works great except when the user doesn’t select a date but hits the button. The page throws an error on the sql string…
<code>
string eventdate;
eventdate = eventCalendar.SelectedDate.ToShortDateString();
</code>
Thanks
Steve
You could always check that a date is selected before firing off the SQL…

That’s what I was thinking. I want to test for an empty string or/and a null string and if it is empty load in the date now. So that if a user doesn’t select a date then today’s date is put in the database. In asp I would do something like…
<code>
if eventdate =“” then
eventdate =date
end if
</code>
I’m just not sure of the syntax for C# (newbie). I guess it would be something like…
<code>
if (eventdate.EmptyString())
{
eventdate = System.DateTime.Now().ToShortDateString();
}
</code>
What do you reckon??
This is one of those things that ASP.NET doesn’t support very well. You can’t just do something as simple as this unfortunately:
if (Calendar1.SelectedDate == null) {
Response.Write("You must select a date first!");
}
Instead, what you could do is place the date into a hidden field or an invisible Label control when the user selects a date from the Calendar. Then when the user selects the button to insert the value into the database, check the Label control for an empty string:
<HTML>
<HEAD>
<title>Validating Calendar Selection</title>
<script runat="server" language="c#">
void display(Object s, EventArgs e) {
Label1.Text = Calendar1.SelectedDate.ToShortDateString();
}
void validate(Object s, EventArgs e) {
if (Label1.Text == "") {
Response.Write("You must select a date first!");
} else {
Response.Write("Insert into database");
}
}
</script>
</HEAD>
<body>
<form runat="server">
<asp:Calendar ID="Calendar1" Runat="server" OnSelectionChanged="display"></asp:Calendar><br><br>
<asp:Button ID="Button1" Runat="server" Text="Button" OnClick="validate" />
<asp:Label ID="Label1" Runat="server" Visible="false" />
</form>
</body>
</HTML>
A bit of a tricky one then. I took your suggestion and tried this…
<code>
public void AddEvent(object sender, System.EventArgs e)
{
string eventdate, location, details,author,sqlString=“”;
eventdate = eventCalendar.SelectedDate.ToShortDateString();
secretLabel.Text = eventCalendar.SelectedDate.ToShortDateString();
location = eventLocation.Text.Replace("'","''");
details=eventDetails.Text.Replace("'","''");
author = Session["name"].ToString();
if (secretLabel.Text == "")
{
sqlString="insert into LeaderEvents "+
"(eventdate,location,details,addedby) "+
"values "+
"(getDate(),'"+location+"',+'"+details+"','"+author+"')";
}
else
{
sqlString="insert into LeaderEvents "+
"(eventdate,location,details,addedby) "+
"values "+
"('"+eventdate+"','"+location+"',+'"+details+"','"+author+"')";
}
SqlConnection conn = new SqlConnection();
SqlCommand command = new SqlCommand(sqlString,conn);
conn.ConnectionString=ConfigurationSettings.AppSettings["leader_dsn"];
conn.Open();
command.ExecuteNonQuery();
conn.Close();
entryPanel.Visible=false;
thanksPanel.Visible=true;
}
</code>
thinking that I would cahange the sql string according to the if condition but the first bit of the if doesn’t seem to run so I think the if (secretLabel.Text == “”) is not picking up the non click. Maybe the calendar inserts a null value??
Steve
P.S thank you guys for your responses. I’ll get this app built yet!!
Solved my problem. I added a page load event that gives the calendar the default date. That way the user doesn’t have to select…
private void Page_Load (Object s, System.EventArgs e)
{
if (!IsPostBack)
{
eventCalendar.SelectedDate=System.DateTime.Now;
}
}
Steve