Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hello everyone!

 

i am trying to use a calendar control. basic function, really. i want to click on a date, query a database for events for which the date is the same, and then display the events.

 

my problem is that i can't find the way to extract the date from the calendar control to pass it as a parameter to my sqlcommand object.

 

i set a breakpoint on the SelectionChanged sub, but when i run it, my code never actually stops at that event.

 

ok, here's some code :

 

private DateTime _dt;
private void Page_Load(object sender, System.EventArgs e)
{
//on first execution, we just use today's date
if (!this.IsPostBack)
{
	_dt = DateTime.Now;
}
	LoadEvents();
}

private void LoadEvents()
{
string strSQL = "Select * from Events WHERE eventStartDate = @date order by eventStartDate asc";
SqlConnection conn = new SqlConnection(myConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(strSQL,conn);
SqlParameter prm = new SqlParameter();
prm.SqlDbType = SqlDbType.DateTime;
prm.Size=8;
prm.ParameterName = "@date";
prm.Value = _dt;
cmd.Parameters.Add(prm);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
	{
		Response.Write(dr["eventTitle"]);
	}
dr.Close();
conn.Close();
}

private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
// this code is never executed ... is it in the wrong event ?
_dt = Calendar1.SelectedDate;
}

 

yes, i'm using code-behind. any hints are welcome. thank you !!

 

Huby.

there are 10 kinds of people on earth:

those who understand binary, and those who don't.

Posted
Do you have the following line in InitializeComponent()

this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);

 

yes, i do :(

 

i followed the process step-by-step and the calendar control always returns the value '1/1/1' ...

 

i suppose it loses its value on Postback, but i don't know how i can have him return the date i actually clicked ...

 

thank you for the reply, any more hints are more than welcome !!

 

peace,

Huby.

there are 10 kinds of people on earth:

those who understand binary, and those who don't.

Posted
Have you tried placing the LoadEvents call within the IsPostBack block

 

lol i just logged on to tell you it works, and that is exactly how i solved it :D

 

thanks anyway !!

 

huby.

there are 10 kinds of people on earth:

those who understand binary, and those who don't.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...