Solved

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

Posted on 2011-09-07
3
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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