Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-07
3
Medium Priority
?
230 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:mainrotor
3 Comments
 
LVL 18

Accepted Solution

by:
ivan_vagunin earned 2000 total points
ID: 36498520
Hi! Try following:
foreach(string error in ValidateData())
{
    lblErrors.Text += error + "<br/>";
}
0
 
LVL 2

Expert Comment

by:AJRDev
ID: 36498567
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

0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 36498659
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

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question