C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Transfering data from textbox to listbox.


Posted Date: 05 Jul 2008      Total Responses: 4

Posted By: Greg       Member Level: Bronze     Points: 1



Hi,

ERROR:INDEX OUT OF RANGE

protected void Button2_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i <= ListBox1.Items.Count; i++)
{
if (TextBox1.Text!=ListBox1.Items[i].Text)
{
ListBox1.Items.Add(TextBox1.Text);
}
else
{
Response.Write("repeated");
}
}





Responses

Author: arun kumar    05 Jul 2008Member Level: SilverRating:     Points: 3

ListItem li = new ListItem(TextBox1.Text);
if (!ListBox1.Items.Contains(li))
{
ListBox1.Items.Add(TextBox1.Text);
}
else
{
Response.Write("repeated");
}



Author: mohan    06 Jul 2008Member Level: SilverRating:     Points: 3

Your error is index is out of range. It is because listbox has N items you are counting them from 0. So the last listitem index is N-1.But you are counting upto N.So it is giving as error.

Let me know it is useful to u or not.......



Author: Sabu C Alex    06 Jul 2008Member Level: GoldRating:     Points: 3

hi friend,
try this code

int i;
for (i = 0; i <= ListBox1.Items.Count-1; i++)
{
if (TextBox1.Text!=ListBox1.Items[i].Text)
{
ListBox1.Items.Add(TextBox1.Text);
}
else
{
Response.Write("repeated");
}
}



Author: Jessie    07 Jul 2008Member Level: GoldRating:     Points: 2

The "=" in "i <= ListBox1.Items.Count" is the culprit. Remove it
and rewrite as

i<ListBox1.Items.Count



Post Reply
You must Sign In to post a response.
Next : Session Timeout Redirect
Previous : adding data from 1 listbox 2 another on selectedindexchange
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use