Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:17:28+00:00 2026-05-25T15:17:28+00:00

I have one string collection and I am executing foreach loop on the string

  • 0

I have one string collection and I am executing foreach loop on the string collection. So in foreach I want to bind value to dropdownlist using LINQ query. That time for loop executed without error, but binding in dropdownlist is overriding the value.

Please check my snippet:

public void binddrop(StringCollection colle)
{
    foreach (string str in (StringCollection)colle)
    {
        string[] user = str.Split('|'); 

        iPhoneDataContext objdata = new iPhoneDataContext();

        var userdetails = (from users in objdata.UserDetails.AsEnumerable()
                           where users.UserType != null && users.Email==user[2].ToString()
                           select new
                           {
                               Name = users.FirstName,
                               ID = users.UserId
                           }) 

        drpvendor.DataSource = userdetails;
        drpvendor.DataTextField = "Name";
        drpvendor.DataValueField = "ID";
        drpvendor.DataBind();
    }
     drpvendor.Items.Insert(0, new ListItem("-Select Vendor-", "0"));
}

For some reason the for loop for StringCollection overrides the value in dropdownlist. So how could I bind in another way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:17:29+00:00Added an answer on May 25, 2026 at 3:17 pm

    The reason that the value gets overwritten is because you are doing the binding inside the foreach. Basically on first foreach cycle you are getting first userdeails and then binding it to the DDL, on next cycle you are binding the 2nd userdetails to the DDL and so on, so what you get in the end is just the last userdetails in the DDL.

    What you need to do is to get all the userdetails in a collection and then databind the DDL onto that collection.

    You need to do something like this:

    [Index.aspx.cs]

    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    
    namespace DropDownListBinding
    {
        public partial class Index : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                binddrop();
            }
    
            public void binddrop()
            {
                // this is the collection that will be bound to the DDL
                var userdetailsCollection = new List<object>();
    
                // generate some userdetails and add them to the collection
                for (var i = 0; i < 3; i++)
                {
                    var userdetails = new
                    {
                        Name = "User" + i,
                        ID = i.ToString()
                    };
                    userdetailsCollection.Add(userdetails);
                }
    
                // now we can bind the DDL to the collection
                drpvendor.DataSource = userdetailsCollection;
                drpvendor.DataTextField = "Name";
                drpvendor.DataValueField = "ID";
                drpvendor.DataBind();
    
                drpvendor.Items.Insert(0, new ListItem("-Select Vendor-", "0"));
            }
        }
    }
    

    [Index.aspx]

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="DropDownListBinding.Index" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
          <asp:DropDownList runat = "server" ID="drpvendor" />
        </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a string that I have been using in one aspx.cs
I have one JSON that is coming in a string format. I need to
If I have a string formatting program in C, that consists of only one
I'm using .NET 3.5. I have two string arrays, which may share one or
I have a function which parses one string into two strings. In C# I
I have a simple web service operation like this one: [WebMethod] public string HelloWorld()
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I have a very simple class with only one field member (e.g. String). Is
I have a one-dimensional array of strings in JavaScript that I'd like to turn
Imagine I have an function which goes through one million/billion strings and checks smth

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.