Search code examples
c#.netvb.netarraylistdatareader

Accessing my Class and presenting the output on the presentation layer


I have a class that I have written to run a stored procedure, start a datareader and put the contents into a arraylist.

CLASS CODE

public class GHPSlider
{
    private clsDbAccess _db;
    private clsDbAccess _tmpDb;

    public ArrayList getBanners(int thisBannerType)
    {
        ArrayList HeroBanners = new ArrayList();
        SqlDataReader _dr = null;
        SqlCommand cmd = _db.Command;
        if (_db.Connection.State == ConnectionState.Closed)
        {
            _db.Connection.Open();
        }
        cmd = _db.Command;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetGHPSlides";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@ID", thisBannerType);
        _dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (_dr.HasRows)
        {
            while (_dr.Read())
            {
                object[] values = new object[_dr.FieldCount];
                _dr.GetValues(values);
                HeroBanners.Add(values);
            }
        }
        _dr.Close();

        return HeroBanners;
    }
}

On my code-behind I am trying to access the ArrayList so that I can loop through it and output the code to the presentation layer. I am fairly new to the n-tier way of programming and could use some help to get me in the right direction.

CODE-BEHIND CODE

Public Function GetHeros() As String

    Dim thisTestSubject As ArrayList = New GHPSlider.getBanners(1)
    'Loop through thisTestSubject ArrayList and place into HTML string to pass to literal or label
    thisBannerSlideBuilderHTML = "<div style='z-index:0;background-color:#FFF;'><a href='" + thisBannerSlideURL.ToString() + "' onclick='RecordBannerClick(this, 'Hero', 'GHP', '" + thisBannerSlideTitle.ToString() + "');'><img alt='" + thisBannerSlideTitle.ToString() + "' src='" + thisBannerSlideImagePath.ToString() + "' /></a></div>";
    'Place Inside Literal or Label to display to presentation layer

End Function

If someone could help point me in the right direction, it would be wonderful! Thanks in advance.


Solution

  • If you're unable to access the class that you have, you're going to need to instantiate it. For instance,

    GHPSlider slider = new GHPSlider(); 
    

    From there, you'll be able to access properties and methods with

    slider.getBanners();
    

    The code you are currently using is pretty cryptic, I'm not sure what you're using it for, so it may be viable. However, you may want to read a bit about layering programs, it'll help you mentally organize this stuff a bit. I know you don't really have a Data Layer, but reading about Data Access Layers will probably help you to understand the architecture behind it.


    I can only really provide help from the C# standpoint, but you may be able to port it over.

    For iterating through the values of an array, it really depends on the purpose. In simplistic coding, let's say you had an array of ten values. Iterating through them with a for loop will let you get each value, by passing it to another command.

    int[] array = new int[p];
    
            for (int i = 0; i < p; i++)
            {
                array[i] = i;
                Console.WriteLine(array[i]);
            }
    

    The output of this is a count from 0-9.

    Help with arrays!

    Now, more specifically, ArrayList

    I understand you are using older code, hence, ArrayList, but the iteration of values with a for loop is probably going to be your best bet.