.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Populating DropDownList in FormView from Code Behind

Posted By:      Posted Date: October 25, 2010    Points: 0   Category :ASP.Net

On my formview I have a drop down list that is populated with the names of the files in a certain directory. This works fine, but when the drop down list selected value is bound to the database field I get the following error :

'ImageFileDropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

I have setup other drop down lists in the same formview populated from SqlDataSources and these all work fine. I can see perhaps why this is an issue because I have not specified a DataValueField or a DataTextField for this drop down that is populated in code (whereas I can for the SQL bound ones) because I'm not sure what these settings would be.

I need to have a "blank" option as well so that the field does not default to the first value in the list.

My code (I have stripped out some meaningless controls) :

<asp:FormView ID="FormView1" runat="server" DataKeyNames="BannerID" DataSourceID="SqlDataSource2" OnItemDeleted="FormView1_ItemDeleted">
    <table width="100%" cellspacing="0" cellpadding="4">                          

View Complete Post

More Related Resource Links

DropDownList within FormView Error

I have a FormView which works find and I have added a new DropDownList within the form which references another table to which I need to select a value,
I have added the dropdownlist2 and setup a new connection string to this, when I run the page I get this error:

Could not find control 'DropDownList2' in ControlParameter 'CurrencyID'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Could not find control 'DropDownList2' in ControlParameter 'CurrencyID'.
any help would be greatly

Help with displaying formview based on dropdownlist


I have a formview1 with a dropdownlist1 in the insert template. Based on the value of that dropdownlist I would like to display a second formview on the same page in insert mode so that I can write data to two different tables.

How can I accomplish this?

Is this the best way to do it?



Formview PagerTemplate DropDownList Navigation


Does anyone know if its possible to use a DropDownList within the PagerTemplate to navigate to a specific record?  I have been able to place the DropDownList outside of the FormView and PagerTemplate and make it work, however, I can't get it to work inside the PagerTemplate.


Dropdownlist appears to be null in code behind


I have a weird problem...

I have a web part form, with one of the dropdownlist, when referenced in code behind, happes to be null.

Common.bindMultiChoices(ddlPly4, oEnquiriesList, "Ply 4 Type (Paper)", "Select Ply 4", null); 

But at front user control file, I do have:

<tr><td><asp:DropDownList ID="ddlPly4" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlPly4_OnSelectedIndexChanged" /&

Clear a DropDownList from code behind.

Hi All,I want to clear the value of DropDownList from codebehind and i have found followingsolution.ddl.Items.Clear(); andddl.DataSource=null; ddl.DataBind();Now the question is which is the best solution and why?

FormView EditItemTemplate DropDownList loaded from codebehind via stored procedure

I'm attempting to load DropDownLists from the C# codebehind as the FormView switches modes to Edit and displays the EditItemTemplate.  I present the data in a FormView ItemTemplate using text boxes.  When the user selects Edit, I have been attempting to load the EditItemTemplate dropdownlists by creating a dataset and binding it to the control, without success.  I've successfully loaded them in previous projects from within the .aspx code using <asp:ListItem> and within the C# code behind using Control.Items.Add(new ListItem), which are essentially the same thing of course .  This project is different in that I want to get the data from a stored procedure on a MSSQL server. In my latest attempt, I can see the data in the datasets.  I'm getting "Error connecting to database.'eitRace' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value" I include the code below.  If someone could point me to an example I would be greatful.  There has to be an elegant way of accomplishing this. Thank You, rlbaum *************************************************** <EditItemTemplate> <asp:Button ID="updatebutton" runat="server" CausesValidation="false" Text="Update&quo

get value from any item in FormView > vb code .

hi. i have this :label_show and formview1 and a button1.and a label_name in formview1 .i wana when i pressed button1 >>  label_show.text  =  formview1.label_name.texthow i can do it ?

SqlDataSource, FormView, and Update with stored procedure in code behind

Hello, Using a FormView with a SqlDataSource, I'm attempting to Update data by calling a stored proc in code behind. I was having trouble getting parameters using Update Parameters in the SqlDataSource, but found a working solution by coding the parameters. The problem now is I'm getting an "Updating is not supported by data source 'XYZ' unless UpdateCommand is specified'. I saw some previous posts on the forums, but didn't find them very enlightening. 

DropDownList inside FormView Insert Template

Hi, I am trying to change the textboxes in the FormView insert template to dropdownlists but they keep setting the parameters to null.   Here's what I have:  <asp:DropDownList ID="ddlYearOfQualification" SelectedValue='<%# Bind("YearOfQualification") %>' runat="server"> </asp:DropDownList> In Page_Load CType(frmSurvey.FindControl("ddlYearOfQualification"), DropDownList).Items.Add(New ListItem("2010", "2010"))CType(frmSurvey.FindControl("ddlYearOfQualification"), DropDownList).Items.Add(New ListItem("2009", "2009"))   and in the SqlDataSource InsertCommand="INSERT INTO [tblSurvey] (... [YearOfQualification] ...) VALUES ( ... @YearOfQualification ... ) And in insert parameters: <asp:Parameter Name="YearOfQualification" Type="Int16" /> And the insert fails with YearOfQualification as null. If I sue the standard textbox: <asp:TextBox ID="YearOfQualificationTextBox" runat="server"                 Text='<%# Bind("YearOfQualification") %>' /> It works.   Any ideas? Many thanks in advance.

DropDownList in Formview has events masked

Hello all, I'm new to ASP.Net (using 2010 pro version) and I have a problem with ASP.NETthat is giving me fits trying to solve it. I am trying to catch the SelectedIndexChanged event on the ddlParent DropDownList and use it's SelectedValue in code behind to repopulate the ddlChild DropDownList.  I've distilled it down to some very simple test code and I'd appreciate some understanding of why this is not working as expected and what I should do to make it work (In VB please). I get the following compiler error: Compiler Error Message: BC30456: 'ddlParent_SelectedIndexChanged' is not a member of 'ASP.test_aspx'. Test Case code follows: <%@ Page Language="vb" AutoEventWireup="True" CodeBehind="Test.aspx.vb" Inherits="WebApplication2.Test" %> <!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 id="Head1" runat="server">    <title></title></head><body>    <form id="form1" runat="server">           <asp:DropDownList ID="ddlParent" runat="server"   &nbs

DataList/FormView won't populate DropdownList

I have a ddl inside the formview that I can't figure out how to wire up properly. The formview contains detail info, but the ddl needs to list the entire set to items. Then I want the ddl's selected value be set from the recordset intended  for the formview. I think it's just a timeing issue s I would like your help. Thanks, - EJM. Here my code somewhat reduced to save space...<asp:DataList Width="700" ID="DlMembers" runat="server" DataKeyField="Ident" OnItemCommand="DlMembers_ItemCommand" OnDeleteCommand="DlMembers_DeleteCommand" > <HeaderTemplate> <table border="1" cellpadding="2" cellspacing="0"> <tr align="left" valign="top"> <th nowrap>First Name</th><th nowrap>Last Name</th><th nowrap>Address</th><th nowrap>Email</th><th nowrap>Home Phone</th><th nowrap>Work Phone</th><th nowrap>Cell Phone</th><th nowrap>Position</th><th nowrap>Admin</th><th nowrap>Comments</th><th> </th><th> </th></tr></HeaderTemplate><ItemTemplate>

Populating DropDownList from Database

Hi there,I'm really struggling with this tonight (this being exactly what it says on the tin, (or the subject!)).  Code I'm working with right now is:If Not Page.IsPostBack Then Dim Category As String Dim CategoryReader As OdbcDataReader = Database.Read("SELECT * FROM tblBlogCategory") While CategoryReader.Read() Category = CategoryReader("Category").ToString() End While DropDownList1.DataSource = Category DropDownList1.DataBind() End IfI've also tried things likeDropDownList1.DataSource = Database.Read("SELECT * FROM tblBlogCategory") but it just stubbornly refuses to populate the list (i.e. the pagealways loads with a blank drop down list).  An idea I'm playing around with is putting the values of the Category table into an array, but it seems to have a problem with that as well.  Hope someone can advise me and point me in the right direction.  Feel free to be critical of my code too.ThanksDouglas

formview + dropdownlist



I have a table which has several categories like animals, birds etc.

When I select animals in dropdown I should get all the details of all animals which i have in database should display in formview. How can i do tht using C#?

dynamically populating the database in the dropDownlist using c# in asp.net


hi everyone , plz listen my question very carefully

i have 2 tables  Desgn & EmpForm

Desgn table is lik this

CREATE TABLE [dbo].[Desgn](
    [DesignationCode] [int] IDENTITY(1,1) NOT NULL,
    [DesignationName] [varchar](50) NOT NULL,
    [DesignationCode] ASC

EmpForm table is like this

CREATE TABLE [dbo].[Empform](
    [EmpId] [varchar](50) NOT NULL,
    [EmpName] [varchar](50) NOT NULL,
    [EmpSalary] [numeric](18, 0) NULL,
    [EmpDesignationcode] [numeric](18, 0) NOT NULL,
    [EmpDateofBirth] [date] NOT NULL

now my ques is i want to dynamically populate the dropdown list of designation on the basis of the code but instead of code name should get populated .i have written the code like this 

public void Populate1()
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["co

AJAX DropDownList won't populate.. error500.. code is working though.


Here's what I have.  I was able to debug and traverse through the webservice and return teh array back into the first call method of: return new CarsService()..  But back in the page_load method, I can't get it to update and show up in the page.  what am I doing wrong?!?!


<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                    <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
                     Category="Make" PromptText="Please select a make" LoadingText="[Loading makes...]"  

Items added through Javascript to a dropdownlist are not accessible through code behind


Hi All,

I am successfully able to add new items to a drop down list using Javascript.

The script I am using is 

        function FillJavaScriptValues() {
            var ddlDeliveryVan;
            ddlDeliveryVan = document.getElementById("DropDownList1");
            var opt = document.createElement("option");
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend