user1608084
user1608084

Reputation: 1

Dropdownlist becomes empty when selection changed

I am new to c# and asp.net. I have a GridView with certain records. When any record is chosen it is displayed in the DetailsView. My details view has 3 DropDownLists. In the edit mode of my details view, when I changed my selection in the drop down list and update it, the particular field takes null values. I have used OnSelectedIndexChanged and I am sure its getting triggered but the values is null. Here is my code.

protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
    DropDownList ddl = (DropDownList)this.form1.FindControl("DropDownList1");
    hi.Visible = true;
    if (ddl != null)
    {
        ddl.Text = ddl.SelectedItem.Value;
        ddl.DataBind();
    }     
}

I have also tried using the SelectedValue property to update but that didnt work too.

<asp:DropDownList ID="DropDownList1" DataTextField="strCollegeLongName"
    DataValueField="strCollegeLongName" runat="server" 
    DataSourceID="SqlDataSource1" autoPostBack="true" AppendDataBoundItems="false" 
    SelectedValue='<%#Bind="College">%'>
</asp:DropDownList>

UpdateCommand="UPDATE [RegistrationFormInfo] SET [FirstName]=@FirstName,
    [LastName]=@LastName,[EmailAddress]=@EmailAddress,[GradDate]=@GradDate,
    [WorkAuth]=@WorkAuth,[Address]=@Address,[Phone]=@Phone,[UFID]=@UFID,
    [GatorAcct]=@GatorAcct,[College]=@College,[Major]=@Major,[UserType]=@UserType,
    [AdminUser]=@AdminUser,[blnActive]=@blnActive,[EntryTS]=@EntryTS WHERE [Guid]=@Guid "

So, here it throws an error saying @College doest not exist, but my table has College column.

I am stuck and any help will be greatly appreciated!

Upvotes: 0

Views: 782

Answers (3)

el vis
el vis

Reputation: 1302

Instead of DropDownList ddl = (DropDownList)this.form1.FindControl("DropDownList1");, try this:

DropDownList ddl = (DropDownList)sender;

`

Upvotes: 1

pseudocoder
pseudocoder

Reputation: 4392

Calling ddl.DataBind(); will reset the SelectedIndex. To avoid circular logic in your code it's a bad idea to call DataBind() in a ddl's SelectedIndexChanged event handler.

Upvotes: 1

Anssssss
Anssssss

Reputation: 3262

Well, the @College error is probably about the named parameter not being provided, not about the column being missing.

Upvotes: 1

Related Questions