Could someone help me, figure out why is that my new table always display the last row of data only.

the code goes like this:

string connection_string = @"Data Source=.;Initial Catalog=calendar;Integrated Security=True";
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataReader dr;
    string cssMonth = "calMonth";
    int i = 0;
    string[] dateTo;
    string[] dateFrom;
    string[] events;

    private void displayCalendarEvents()
    {
        TableRow tRow = new TableRow();
        TableCell cellMonth = new TableCell();
        TableCell cellDateTo = new TableCell();
        TableCell cellDateFrom = new TableCell();
        TableCell cellEvents = new TableCell();

        int num = setEvents(); // the number of selected events in the database i.e. 2 rows
        
        for (int i = 0; i < num; i++)
        {
            tRow.CssClass = cssMonth;

            cellDateTo.Text = dateTo[i].ToString();
            cellDateFrom.Text = dateFrom[i].ToString();
            cellEvents.Text = events[i].ToString();

            tRow.Cells.Add(cellDateTo);
            tRow.Cells.Add(cellDateFrom);
            tRow.Cells.Add(cellEvents);
            calendarTable.Rows.Add(tRow);
        }
        

    }

the "calendarTable" is an asp:Table control

here is the code for the setEvents() method

private int setEvents()
    {
        int dataLength = 0;
        conn = new SqlConnection(connection_string);
        conn.Open();

        string strSelect = "SELECT dateFrom, dateTo, event FROM calendarevents";
        cmd = new SqlCommand(strSelect, conn);
        dr = cmd.ExecuteReader();
        if (dr.HasRows == true)
        {
            i = 0;
            dataLength = countRows("calendarevents");
            dateFrom = new string[dataLength];
            dateTo = new string[dataLength];
            events = new string[dataLength];
            while (dr.Read())
            {
                dateFrom[i] = dr["dateFrom"].ToString();
                dateTo[i] = dr["dateTo"].ToString();
                events[i] = dr["event"].ToString();
                i++;
            }
            dr.Close();
        }
        conn.Close();
        return dataLength;        
        
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.