Solved

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

Posted on 2011-09-07
3
217 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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Get String split 5 33
IO.FileSystemWatcher dies when used in Powershell 1 27
Visual Studio 2013 debugging 2 24
C# TextBox 11 15
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now