Mal
Mal

Reputation: 525

Repeated in ASP.NET

The following code displays only the last record from the database. How to display all records?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;
using System.Data.Odbc;

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

        string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=softmail;" + "UID=root;" + "PASSWORD=********;" + "OPTION=3";
        OdbcConnection MyConnection = new OdbcConnection(MyConString);

        MyConnection.Open();
        OdbcCommand cmd = new OdbcCommand("Select name, email, website, comments from awm_comments", MyConnection);
        OdbcDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows == false)
        {
            throw new Exception();
        }
        while (dr.Read())
        {
            if (!IsPostBack)
            {
                string a = dr[0].ToString();
                string b = dr[1].ToString();
                string c = dr[2].ToString();
                string d  = dr[3].ToString();
                ArrayList values = new ArrayList();
                values.Add(new PositionData(a, b, c, d));

                Repeater1.DataSource = values;
                Repeater1.DataBind();

                Repeater2.DataSource = values;
                Repeater2.DataBind();
            }
        }
    }
    public class PositionData
    {
        private string name;
        private string ticker;
        private string val3;
        private string val4;

        public PositionData(string name, string ticker, string val3, string val4)
        {
            this.name = name;
            this.ticker = ticker;
            this.val3 = val3;
            this.val4 = val4;
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public string Ticker
        {
            get
            {
                return ticker;
            }
        }
        public string Val3
        {
            get
            {
                return val3;
            }
        }
        public string Val4
        {
            get
            {
                return val4;
            }
        }
    }
}

Upvotes: 0

Views: 108

Answers (2)

Dexter
Dexter

Reputation: 18452

At the moment you are creating a new ArrayList for each record; then, you're binding that new list to the repeater and overwriting the previous binding.

Instead, you need to add all your values to your list and only bind it once you've read all of your data - move your variable declaration before your loop and your DataBind invocation after your loop, like so:

    ArrayList values = new ArrayList();
    while (dr.Read())
    {
        if (!IsPostBack)
        {
            string a = dr[0].ToString();
            string b = dr[1].ToString();
            string c = dr[2].ToString();
            string d  = dr[3].ToString();
            values.Add(new PositionData(a, b, c, d));
        }
    }

    Repeater1.DataSource = values;
    Repeater1.DataBind();

    Repeater2.DataSource = values;
    Repeater2.DataBind();

Upvotes: 1

Hans Kesting
Hans Kesting

Reputation: 39274

You are constantly resetting the values for that repeater.

What you should do is first collect all records into a single ArrayList and only when that is done execute a single DataBind();

Something like:

    if (!IsPostBack)
    {
        ArrayList values = new ArrayList();
        while (dr.Read())
        {
            string a = dr[0].ToString();
            string b = dr[1].ToString();
            string c = dr[2].ToString();
            string d  = dr[3].ToString();
            values.Add(new PositionData(a, b, c, d));
        }

        Repeater1.DataSource = values;
        Repeater1.DataBind();

        Repeater2.DataSource = values;
        Repeater2.DataBind();
    }

Upvotes: 0

Related Questions