Storing records in a dropdownlist from DB without using LINQ Data source

Posted by user1318369 on Stack Overflow See other posts from Stack Overflow or by user1318369
Published on 2012-04-06T23:08:52Z Indexed on 2012/04/06 23:30 UTC
Read the original article Hit count: 155

Filed under:
|
|

I have a website for dance academy where Users can register and add/drop dance classes.

In the web page to drop a particular dance, for a particular user, the dropdown displays her registered dances.
Now I want to delete one of the dances from the list. So i'll remove the row from the table and also from the dropdownlist. The problem is that everytime the item with the lowest ID (index) is getting deleted, no matter which one the user selects. I think I am storing the DataTextField and DataValueField for the dropdown incorrectly. Can someone please help me out? The code is:

private void PopulateDanceDropDown() {

        var registereddanceList = from dd in context.DANCER_AND_DANCE
                                    where dd.UserId == dancerId
                                    select new
                                    {
                                        Text = dd.DanceName,
                                         Value = dd.DanceId
                                    };

        dances.DataSource = registereddanceList;
        dances.DataTextField = "Text";
        dances.DataValueField = "Value";
        dances.DataBind();

    }

    protected void dropthedance(object o, EventArgs e)
    {
        String strDataValueField = dances.SelectedItem.Value;            
        int danceIDFromDropDown = Convert.ToInt32(strDataValueField);
        var dancer_dance = from dd in context.DANCER_AND_DANCE
                           where dd.DanceId == danceIDFromDropDown
                           select dd;



        foreach (var dndd in dancer_dance)
        {
            context.DANCER_AND_DANCE.DeleteOnSubmit(dndd);

        }

        try
        {
            context.SubmitChanges();
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        PopulateDanceDropDown();

    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET