0% found this document useful (0 votes)
24 views

DivByZero IndexOutRange and TreeView

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

DivByZero IndexOutRange and TreeView

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Note : If the Code is for a web page

then make sure you do not mess with


the first two lines of your own aspx
file( HTML to HTML only! ) and also
do not change namespace and class
name in your own c# file!
DivByZero, IndexOutRange and Tree View with sitemap

DivisibleByZeroException and IndexOutOfRangeException

Code :

Aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DivZero.aspx.cs"


Inherits="PracticalTest1.DivZero" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Exception Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Exception Demo</h1>

<label>Enter a number: </label>


<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
<asp:Button ID="btnDivideByZero" runat="server" Text="Divide by Zero"
OnClick="btnDivideByZero_Click" />
<asp:Button ID="btnIndexOutOfRange" runat="server" Text="Index Out of Range"
OnClick="btnIndexOutOfRange_Click" />

<br />

<label>Result: </label>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

C# file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PracticalTest1
{
public partial class DivZero : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnDivideByZero_Click(object sender, EventArgs e)
{
try
{
// Attempt to divide by zero
int number = int.Parse(txtNumber.Text);
int result = 10 / number; // This may throw DivideByZeroException

// Display the result


lblResult.Text = $"Result of division: {result}";
}
catch (DivideByZeroException ex)
{
// Handle DivideByZeroException
lblResult.Text = $"Error: {ex.Message}";
}
catch (Exception ex)
{
// Handle other exceptions
lblResult.Text = $"Error: {ex.Message}";
}
}

protected void btnIndexOutOfRange_Click(object sender, EventArgs e)


{
try
{
// Attempt to access an index that is out of range
int[] numbers = { 1, 2, 3 };
int index = int.Parse(txtNumber.Text);
int result = numbers[index]; // This may throw IndexOutOfRangeException

// Display the result


lblResult.Text = $"Value at index {index}: {result}";
}
catch (IndexOutOfRangeException ex)
{
// Handle IndexOutOfRangeException
lblResult.Text = $"Error: {ex.Message}";
}
catch (Exception ex)
{
// Handle other exceptions
lblResult.Text = $"Error: {ex.Message}";
}
}
}
}

Tree View With SiteMap

- Note : You need to add a sitemap file in your project, using the add >
new items.
After that make 3 more aspx file in the project, named as Default,
Products & ProductsDetail.
Just fill some simple HTML content in these files, like put a H1 and
write some random content.

-Once Done with that fill the below XML content in the sitemap file
created before :

<?xml version="1.0" encoding="utf-8" ?>


<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/Default.aspx" title="Home" description="Home Page">
<siteMapNode url="~/Products.aspx" title="Products" description="View our
products">
<siteMapNode url="~/ProductDetail.aspx?productId=1" title="Product 1"
description="Details of Product 1" />
<siteMapNode url="~/ProductDetail.aspx?productId=2" title="Product 2"
description="Details of Product 2" />
</siteMapNode>
<siteMapNode url="~/Contact.aspx" title="Contact" description="Contact Us" />
</siteMapNode>
</siteMap>

- After that write the below code in your main file other than that
created above!.

Code :
Aspx File
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeViewControl.aspx.cs"
Inherits="PracticalTest1.TreeViewControl" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Navigation Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Navigation Demo</h1>

<!-- TreeView control -->


<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"
ShowExpandCollapse="true">
<DataBindings>
<asp:TreeNodeBinding DataMember="siteMapNode" TextField="title"
NavigateUrlField="url" />
</DataBindings>
</asp:TreeView>

<!-- DataList control -->


<asp:DataList ID="DataList1" runat="server" DataSourceID="SiteMapDataSource1">
<ItemTemplate>
<h2><%# Eval("title") %></h2>
<p><%# Eval("description") %></p>
<hr />
</ItemTemplate>
</asp:DataList>

<!-- SiteMapDataSource control -->


<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"
ShowStartingNode="false" />
</div>
</form>
</body>
</html>

C# File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PracticalTest1
{
public partial class TreeViewControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind the TreeView and DataList controls to the SiteMapDataSource
TreeView1.DataBind();
DataList1.DataBind();
}
}
}
}

You might also like