Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

680 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