Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have downloaded the C# code for ctlCalendar control. I have deployed the coding on visual studio 2005 C#. When I run the solution is giving me an error on line 36. The error message is ArgumentOutOf RangeException was unhandled by user code.

Please assist the code behind of the page is below


C#
namespace purchaseorders
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
   

	/// <summary>
	///		Summary description for ctlCalendar.
	/// </summary>
	public partial class ctlCalendar : System.Web.UI.UserControl
	{

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				this.TextBox1.Text = System.DateTime.Now.ToShortDateString();
				this.pnlCalendar.Attributes.Add("style","DISPLAY: none; POSITION: absolute");
			}
			else
			{
              
                string id = Page.Request.Form["__EVENTTARGET"].Substring(0, Page.Request.Form["__EVENTTARGET"].IndexOf(":"));
				if (id != this.ID) 
				{
					this.pnlCalendar.Attributes.Add("style","DISPLAY: none; POSITION: absolute");
				}
				else
				{
					this.pnlCalendar.Attributes.Add("style","POSITION: absolute");
				}

			}
			Page.RegisterClientScriptBlock("Script_Panel" + this.ID, "<script> function On"+this.ID+"Click() {  if("+this.ID+"_pnlCalendar.style.display == \"none\")     "+this.ID+"_pnlCalendar.style.display = \"\";   else    "+this.ID+"_pnlCalendar.style.display = \"none\"; } </script>");			
			this.Button1.Attributes.Add("OnClick","On"+this.ID+"Click()");
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
		{
			this.TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
			this.pnlCalendar.Attributes.Add("style","DISPLAY: none; POSITION: absolute");
		}

	}
}


HTML code is below


ASP.NET
<%@ Control Language="c#" Inherits="purchaseorders.ctlCalendar" CodeFile="ctlCalendar.ascx.cs" %>
<asp:textbox id="TextBox1" runat="server" ReadOnly="True" CssClass="text"></asp:textbox>
<input type="button" id="Button1" runat="server" value="..." class="text"><br>
<asp:Panel id="pnlCalendar" runat="server">
	<asp:calendar id="Calendar1" runat="server" BackColor="White" Width="200px" DayNameFormat="FirstLetter"
		ForeColor="Black" Height="180px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#999999"
		CellPadding="4" onselectionchanged="Calendar1_SelectionChanged">
		<TodayDayStyle ForeColor="Black" BackColor="#CCCCCC"></TodayDayStyle>
		<SelectorStyle BackColor="#CCCCCC"></SelectorStyle>
		<NextPrevStyle VerticalAlign="Bottom"></NextPrevStyle>
		<DayHeaderStyle Font-Size="7pt" Font-Bold="True" BackColor="#CCCCCC"></DayHeaderStyle>
		<SelectedDayStyle Font-Bold="True" ForeColor="White" BackColor="#666666"></SelectedDayStyle>
		<TitleStyle Font-Bold="True" BorderColor="Black" BackColor="#999999"></TitleStyle>
		<WeekendDayStyle BackColor="LightSteelBlue"></WeekendDayStyle>
		<OtherMonthDayStyle ForeColor="#808080"></OtherMonthDayStyle>
	</asp:calendar>
</asp:Panel><BR>
<script>

</script>

The error message is pointing to the line below on code behind

C#
string id = Page.Request.Form["__EVENTTARGET"].Substring(0, Page.Request.Form["__EVENTTARGET"].IndexOf(":"));


Kind Regards
Takalani
Posted
Updated 29-Feb-12 17:12pm
v3

Could you specifically mention what line 36 is?
 
Share this answer
 
Hi,

Well it is kind of hard to say what the problem is, because we don't knwo what is on line 36. Please post some code.

But the general idea of ArgumentOutOfRangeException is: he exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.

Read some more here[^].

Kind regards,
 
Share this answer
 
The exception is caused by the SubString.

i.e:

string id = Page.Request.Form["__EVENTTARGET"].Substring(0, Page.Request.Form["__EVENTTARGET"].IndexOf(":"));

//startIndex plus length indicates a position not within this instance.
// -- OR --
// startIndex or length is less than zero.


According to MSDN:

Exception
ArgumentOutOfRangeException	

Condition
startIndex plus length indicates a position not within this instance.

-or-

startIndex or length is less than zero. 


Read some more here[^]
 
Share this answer
 
v2
the error occur due to the length supplied to substring is -1

below code may help to come out from this.

string id = Page.Request.Form["__EVENTTARGET"].Substring(0, Page.Request.Form["__EVENTTARGET"].IndexOf(""));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900