DropDownList DataBind Creates Duplicate Values _ the Official Microsoft ASP

download DropDownList DataBind Creates Duplicate Values _ the Official Microsoft ASP

of 15

Transcript of DropDownList DataBind Creates Duplicate Values _ the Official Microsoft ASP

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 1/15

    ASP.NET

    Search ASP.NET

    Sign In| Join

    Home

    Get StartedDownloadsWeb PagesWeb FormsMVCSolutions

    FRAMEWORKS

    Web PagesWeb FormsMVCEntity Framework

    TECHNOLOGIES

    Web APISingle Page ApplicationAjaxMobileSignalR

    CURRENT RELEASES

    Visual Studio 2013ASP.NET 4.5ASP.NET MVC 4ASP.NET Web Pages 2

    Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources

    CommunityForums

    Home/ASP.NET Forums/General ASP.NET/Web Forms Data Controls/DropDownList DataBind creates duplicate

    values

    DropDownList DataBind creates duplicate valuesRSS

    18 replies

    Last post Oct 30, 2009 01:52 AM by decker dong - msft

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 2/15

    Previous Thread|Next Thread

    Reply

    ppalubinski

    Member

    49 Points

    175 Posts

    DropDownList DataBind creates duplicate values

    Oct 22, 2009 03:48 PM|LINK

    So I've had this problem come up a few times, and it drives me crazy. I have a form with a dropdownlist thatpopulates from a database. There are other controls on the form which cause an autopostback. However, Ineed to re-bind the dropdownlist each time because there is a databound event attached to its datasource. Whenthis happens, it creates duplicate values in the dropdownlist. Does anyone know how to get around this?

    protected void ddlLessors_SelectedIndexChanged(object sender, EventArgs e) { if (ddlLessors.SelectedIndex > 0) { Wizard1.MoveTo(Wizard1.WizardSteps[Wizard1.ActiveStepIndex + 1]);

    this.SetDropDownList((DropDownList)sender);

    ddlProspects.DataBind(); } }

    protected void ddlProspects_DataBound(object sender, EventArgs e) { using (DbConnection dbConnection = this.dbProviderFactory.CreateConnection()) { dbConnection.ConnectionString = this.connString; dbConnection.Open();

    DbCommand dbCommand = dbConnection.CreateCommand();

    dbCommand.CommandText = "SELECT TOP 1 p.ProspectCode FROMACCOUNTING.PROSPECT p " + "INNER JOIN ACCOUNTING.PROSPECT_TRACT p_t ON p.ProspectCode =p_t.ProspectCode " + "INNER JOIN LANDMAN.TRACT t ON p_t.TractId = t.TractId " +

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 3/15

    "INNER JOIN LANDMAN.LEASE le ON t.TractId = le.TractId " + "INNER JOIN LANDMAN.LESSOR l ON le.LessorId = l.LessorId " + "WHERE l.LessorId = @LessorId; ";

    dbCommand.Parameters.Add(new SqlParameter("@LessorId", ddlLessors.SelectedValue));

    ddlProspects.SelectedValue = (string)dbCommand.ExecuteScalar(); } }

    DropDownList databound

    "Do your damnedest in an ostentatious manner all the time."

    Moved by SGWellens on Oct 22, 2009 05:40 PMReply

    astahir

    Member

    512 Points

    118 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 06:43 AM|LINK

    There must be better ways of doing this but one way around could be to delete all items of the DropDownListbefore its DataBind, i.e.

    //Clear existing items if (DropDownList1.Items.Count > 0) { DropDownList1.Items.Clear(); } //Bind DropDownList here

    Reply

    Ahmish

    Participant

    1084 Points

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 4/15

    312 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 06:53 AM|LINK

    Why are you rebinding your dropdownlist on each postback? Once you bound your dropdownlist, the view stateof dropdowlist will persist values on post backs you dont need to rebind.

    Your code should look like this.

    if (!IsPostBack) {

    ddlUser.DataTextField = "User_Name"; ddlUser.DataValueField = "User_Code"; ddlUser.DataSource = GetDataTable(); ddlIssueToUser.DataBind();

    }

    Hope it will help you out :)

    "Hope its your Solution so Mark it as Answer"

    Ahmad Sheikh Microsoft Valuable Geek :: Freelancer & Consultant http://sharpcontents.blogspot.com

    Reply

    ppalubinski

    Member

    49 Points

    175 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 03:00 PM|LINK

    Well, one thing is that it needs to select a specific value in the dropdownlist based on a database query. So eachtime there is any kind of autopostback, I need to make sure that the right item is still selected. I just stuck it in thedatabound event for the dropdownlist because I didn't know where else to put it. Also, I have a fewdropdownlists that are dependent on the values selected in other dropdownlists before being populated... A usercan go back and change the value of an earlier dropdownlist. If so, then it must rebind the dependentdropdownlist(s).

    "Do your damnedest in an ostentatious manner all the time."Reply

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 5/15

    ppalubinski

    Member

    49 Points

    175 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 03:03 PM|LINK

    Thanks. I've tried to call the clear() method in Page_Load, but it didn't seem to fix the problem. But I think I'mjust calling it in the wrong place. I'll tinker around some more and try find a better place to put it...

    "Do your damnedest in an ostentatious manner all the time."Reply

    tamilcodes

    Contributor

    3140 Points

    690 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 04:05 PM|LINK

    This is happens when ur viewstate is set to true,

    and you are databinding the dropdownlist again and again on every postback,

    paste your code for the first ddl

    SITE STATISTICS LITE - www.tamilcodes.com

    TUTORIAL RIVER - HTML, ASP.NET, PHOTOSHOP, WEB DESIGN TUTORIALSEdited by tamilcodes on Oct 23, 2009 04:06 PMReply

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 6/15

    ppalubinski

    Member

    49 Points

    175 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 04:41 PM|LINK

    Here's all my code beside stuff:

    public partial class _Default : System.Web.UI.Page { private DbProviderFactory dbProviderFactory; private string connString = WebConfigurationManager.ConnectionStrings["TcoConnString"].ConnectionString; private string factory = "System.Data.SqlClient";

    protected void Page_Load(object sender, EventArgs e) { this.dbProviderFactory = DbProviderFactories.GetFactory(this.factory); }

    protected void ddlNext_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddl = (DropDownList)sender;

    if (ddl.SelectedIndex > 0) { Wizard1.MoveTo(Wizard1.WizardSteps[Wizard1.ActiveStepIndex + 1]); this.Wizard1_NextButtonClick(sender, new WizardNavigationEventArgs(Wizard1.ActiveStepIndex - 1, Wizard1.ActiveStepIndex)); } }

    protected void ddlProspects_SelectedIndexChanged(object sender, EventArgs e) { if (ddlLessors.SelectedIndex > 0) { ddlLeases.Enabled = true; lbAddTract.Enabled = true;

    pnlAddTract.Visible = false; } else { ddlLeases.Enabled = false; lbAddTract.Enabled = false;

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 7/15

    pnlAddTract.Visible = true; }

    ddlLeases.DataBind(); }

    protected void lbAddLessor_Click(object sender, EventArgs e) { pnlAddLessor.Visible = true; ddlLessors.Enabled = false; ddlLessors.SelectedIndex = 0; lbAddLessor.Enabled = false; }

    protected void lbClearLessor_Click(object sender, EventArgs e) { pnlAddLessor.Controls.Clear(); pnlAddLessor.Visible = false; ddlLessors.Enabled = true; lbAddLessor.Enabled = true; }

    protected void lbAddTract_Click(object sender, EventArgs e) { pnlAddTract.Visible = true; ddlProspects.Enabled = true; ddlLeases.Enabled = false; lbAddTract.Enabled = false; }

    protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) { switch (e.CurrentStepIndex) { case 0: if (ddlLessors.SelectedIndex > 0) { ddlLeases.Enabled = true; lbAddTract.Enabled = true; pnlAddTract.Visible = false; } else { ddlProspects.SelectedIndex = 0; ddlLeases.SelectedIndex = 0;

    ddlLeases.Enabled = false; lbAddTract.Enabled = false; pnlAddTract.Visible = true; }

    ddlProspects.DataBind();

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 8/15

    ddlLeases.DataBind(); break; } }

    protected void ddlProspects_DataBound(object sender, EventArgs e) { using (DbConnection dbConnection = this.dbProviderFactory.CreateConnection()) { dbConnection.ConnectionString = this.connString; dbConnection.Open();

    DbCommand dbCommand = dbConnection.CreateCommand();

    dbCommand.CommandText = "SELECT TOP 1 p.ProspectCode FROMACCOUNTING.PROSPECT p " + "INNER JOIN ACCOUNTING.PROSPECT_TRACT p_t ON p.ProspectCode =p_t.ProspectCode " + "INNER JOIN LANDMAN.TRACT t ON p_t.TractId = t.TractId " + "INNER JOIN LANDMAN.LEASE le ON t.TractId = le.TractId " + "INNER JOIN LANDMAN.LESSOR l ON le.LessorId = l.LessorId " + "WHERE l.LessorId = @LessorId; ";

    dbCommand.Parameters.Add(new SqlParameter("@LessorId", ddlLessors.SelectedValue));

    string prospectCode = (string)dbCommand.ExecuteScalar();

    if (!string.IsNullOrEmpty(prospectCode)) { ddlProspects.SelectedValue = prospectCode; } else { ddlLeases.Enabled = false; } } }

    protected void ddlLeases_DataBinding(object sender, EventArgs e) { dsLeases.SelectParameters["LessorId"].DefaultValue = ddlLessors.SelectedValue; dsLeases.SelectParameters["ProspectCode"].DefaultValue = ddlProspects.SelectedValue; }

    protected void ddlLeases_DataBound(object sender, EventArgs e) { if (ddlLeases.Items.Count == 0) {

    } } }

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 9/15

    And here's all the markup:

    Lessor:

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 10/15

    First Name: Last Name: Middle Name: Company/Entity Name: Prospect:

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 11/15

    Lease:

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 12/15

    "Do your damnedest in an ostentatious manner all the time."Reply

    sassyboy

    Member

    728 Points

    322 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 07:01 PM|LINK

    Seems you need to use a Cascading DropdownList of the asp.net ajax control toolkit

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/CascadingDropDown/CascadingDropDown.aspx

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Walkthrough/CCDWithDB.aspx

    http://www.asp.net/Learn/ajax-videos/video-77.aspx

    Thanks

    S@ss (Shashi Shekhar Singh)

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 13/15

    My Blog

    Please mark a post as ANSWER if that post helped resolve your query.Reply

    ppalubinski

    Member

    49 Points

    175 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 07:48 PM|LINK

    Okay, I tinkered around with the cascadingdropdown and it seems to be a good approach. The only problem is

    that I still need to it do an autopostback because I need to use the OnSelectedIndexChanged event to triggersome other actions. Also, when it does post back to the server, it doesn't seem to know what the state is of the

    dropdownlist (says SelectedIndex is 0 when it isn't)... Have you come across this problem?

    [WebMethod] public AjaxControlToolkit.CascadingDropDownNameValue[]

    GetDropDownLessors(string knownCategoryValues, string category) {

    using (SqlConnection conn = new SqlConnection( WebConfigurationManager.ConnectionStrings["TcoConnString"].ConnectionString))

    { conn.Open();

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 14/15

    string query = "SELECT LessorId, (LastName + ', ' + FirstName + ' ' + ISNULL(MiddleName,''))Name FROM LANDMAN.LESSOR " +

    "ORDER BY Name ASC; ";

    SqlCommand cmd = new SqlCommand(query, conn);

    SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet();

    da.Fill(ds);

    List cascadingDropDownNameValues = new List();

    foreach (DataRow dr in ds.Tables[0].Rows)

    { string lessorId = dr["LessorId"].ToString();

    string name = dr["Name"].ToString();

    cascadingDropDownNameValues.Add( new AjaxControlToolkit.CascadingDropDownNameValue(name, lessorId));

    }

    return cascadingDropDownNameValues.ToArray(); }

    }

    "Do your damnedest in an ostentatious manner all the time."

    Reply

    ppalubinski

    Member

    49 Points

    175 Posts

    Re: DropDownList DataBind creates duplicate values

    Oct 23, 2009 07:51 PM|LINK

    Also, is it possible to set the SelectedValue property on the CascadingDropDown control dynamically (based onanother database query)?

    "Do your damnedest in an ostentatious manner all the time."

    Prev Next

    1 2Last

  • 7/10/13 DropDownList DataBind creates duplicate values : The Official Microsoft ASP.NET Forums

    forums.asp.net/t/1484337.aspx 15/15

    Previous Thread|Next Thread

    This site is managed for Microsoft by Neudesic, LLC. | 2013Microsoft. All rights reserved.

    Privacy Statement|Terms of Use|

    Site Feedback|Advertise With Us|

    CMS by Umbraco

    Follow Us On:

    Twitter|Facebook

    Microsoft

    Feedback on ASP.NET|

    File Bugs