ArrayList Example In Asp.Net

ArrayList Example In Asp.Net

In this post we will learn how to use ArrayList in asp.net.ArrayList dynamically resizes, as elements are added, it grows in capacity to accommodate them.

Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ArrayList Example In Asp.Net</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<h2>ArrayList Example In Asp.Net</h2>
<form id="form1" runat="server">
<div>
<asp:Button ID="abtn" runat="server" Text="Simple ArrayList" OnClick="abtn_Click" /> 
&nbsp;&nbsp;&nbsp;&nbsp; 
<asp:Button ID="adbtn" runat="server" Text="Dynamic ArrayList" OnClick="adbtn_Click" />

</div>
</form>
</body>
</html>

Code:

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

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

    }
    protected void adbtn_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dev;User ID=sa;Password=hightech");
        con.Open();
        string str = "select top 2 * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        ArrayList emailArray = new ArrayList();
        
        while (reader.Read())
        {
            emailArray.Add(reader["uemail"]);    
        }
        foreach (string email in emailArray)
        {
            Response.Write(email);
            //lbl.Text = ;
            //Console.WriteLine(email);
        }
        reader.Close();
        con.Close();
    }
    protected void abtn_Click(object sender, EventArgs e)
    {
	    ArrayList list = new ArrayList();
	    list.Add(1);
	    list.Add(2);
        list.Add(3);
        list.Add(4);
        foreach (int i in list)
        {
            Response.Write(i);
        }
    }

    
}

One thought on “ArrayList Example In Asp.Net

  1. wechsel pkv

    I’m no longer sure where you are getting your information, but great topic. I must spend some time learning much more or understanding more. Thanks for excellent info I used to be looking for this info for my mission.

Comments are closed.