Link to home
Start Free TrialLog in
Avatar of mainrotor
mainrotor

asked on

I need assistance with a Collection in my ASP.Net application

Hi experts,
I have a collection in my ASP.Net application with C#.Codebehind.  I want to display the values
in my collection in a Label control.  How can I do this?  I tried the following but it gave me an error.
lblErrors.Text = validationErrors.ToString();

Here is the code for my collection.
private Collection<String> ValidateData()
        {
            Collection<string> validationErrors = new Collection<String>();

            if (txtTitle_Edit.Text.Length < 1)
            {
                validationErrors.Add("Please provide a title for the book.");
            }

            if (txtAuthorFirstname_Edit.Text.Length < 1)
            {
                validationErrors.Add("Please enter the Author's last name.");
            }

            if (txtISBN_Edit.Text.Length < 1)
            {
                validationErrors.Add("Please enter the book's ISBN#.");
            }

            if (txtPublisher_Edit.Text.Length  < 1)
            {
                validationErrors.Add("Please enter the publisher's name.");
            }
            return validationErrors;

        }


Thanks in advance
mrotor
ASKER CERTIFIED SOLUTION
Avatar of Member_6283346
Member_6283346

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your validationErrors variable only exists inside the ValidateData() method which is why you can't reference it outside that scope.

You need to do something like this:

private List<String> ValidateData()
{
    List<string> validationErrors = new List<String>();

    if(txtTitle_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please provide a title for the book.");
    }

    if(txtAuthorFirstname_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please enter the Author's last name.");
    }

    if(txtISBN_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please enter the book's ISBN#.");
    }

    if(txtPublisher_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please enter the publisher's name.");
    }
    return validationErrors;
}


private void button1_Click(object sender, EventArgs e)
{
    List<String> validationErrors = ValidateData();
    StringBuilder sb = new StringBuilder();

    foreach(String s in validationErrors)
    {
        sb.Append(s);
        sb.Append(" : "); // Or whatever other formatting you want.
    }

    lblErrors.Text = sb.ToString();
}

Open in new window

Avatar of Albert Van Halen
I would use a generic list, convert it to an array and then use String.Join
private List<String> ValidateData()
{
    List<string> validationErrors = new List<String>();

    if(txtTitle_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please provide a title for the book.");
    }

    if(txtAuthorFirstname_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please enter the Author's last name.");
    }

    if(txtISBN_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please enter the book's ISBN#.");
    }

    if(txtPublisher_Edit.Text.Length < 1)
    {
        validationErrors.Add("Please enter the publisher's name.");
    }
    return validationErrors;
}


private void button1_Click(object sender, EventArgs e)
{
    lblErrors.Text = String.Join("<your seperator>", ValidateData().ToArray());
}

Open in new window