1

I have created two TextBoxes to enter the FirstName and LastName of an employee in a web-based ASP.NET application using C# on Visual studio 2010. There is a Button. when I click on it, the values that I enter in the TextBoxes should be displayed in a Gridview without those values being stored in the database. I have already implemented it using ArrayList as shown below. But now I want to implement it using Generic List (Typed List). How can I do that? Can you provide a sample code to execute the above mentioned functionality?

How can I tweak the code given below, to implement the functionality using Generic List (Typed List)?

**

    protected void btnTextDisplay_Click(object sender, EventArgs e)
    {
        ArrayList arr;

        if (Session["array"] == null)
        {
            arr = new ArrayList();
        }
        else
        {
            arr = (ArrayList)Session["array"];
        }

        arr.Add(txtName.Text + "," + txtCity.Text); //store textbox values in the array list
        Session["array"] = arr;

        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("City");

        for (int i = 0; i < arr.Count; i++)
        {
            string[] arrVal;
            arrVal = arr[i].ToString().Split(',');
            dt.Rows.Add(arrVal[0], arrVal[1]);
        }

        gvDisplay.DataSource = dt;
        gvDisplay.DataBind();
    }

**

0

1 Answer 1

1

You can use List<string> instead of ArrayList

protected void btnTextDisplay_Click(object sender, EventArgs e)
    {
       List<string> list;

        if (Session["list"] == null)
        {
            list = new List<string>();
        }
        else
        {
            list = (List<string>)Session["list"];
        }

        list.Add(txtName.Text + "," + txtCity.Text); //store textbox values in the array list
        Session["list"] = list;

        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("City");

        for (int i = 0; i < list.Count; i++)
        {
            string[] arrVal;
            arrVal = list[i].ToString().Split(',');
            dt.Rows.Add(arrVal[0], arrVal[1]);
        }

        gvDisplay.DataSource = dt;
        gvDisplay.DataBind();
    }

A much better approach could be:

If you define a class as:

class Employee
{
    public string FirstName { get; set; }
    public string City { get; set; }
}

and then:

    protected void btnTextDisplay_Click(object sender, EventArgs e)
    {
        List<Employee> list;
        if (Session["list"] == null)
        {
            list = new List<Employee>();
        }
        else
        {
            list = (List<Employee>)Session["list"];
        }
        list.Add(new Employee() { FirstName = txtName.Text, City = txtCity.Text }); //store textbox values in the array list
        Session["list"] = list;
        gvDisplay.DataSource = list; //directly bind the list to the grid
        gvDisplay.DataBind();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.