Solved

ASPX Drop Down List Databind (Multiple DataSources)

Posted on 2013-01-25
5
863 Views
Last Modified: 2013-01-27
Hello,

Today I am using Visual Studio Web Developer Express 2008.  I am building an aspx page with 3 drop down lists within an InsertItemTemplate.

Each of the drop down lists are databound to their own SqlDataSources.  Selecting a value in dropdownlist1 updates the values of dropdownlist2 via autopostback.  The same happens with DropDownList3 when a value is selected from DropDownList2.  This is working nicely.

I want to take the selected values in the 3rd drop down list within the formview and insert them into the database when the insert button is clicked.  i thought something like this would work but I am getting this error on the page when changing the value in any of the 3 drop downs.

"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

Can someone please help?

                        <asp:DropDownList 
                            ID="DropDownList1" 
                            runat="server" 
                            AutoPostBack="True" 
                            CssClass="ddl" 
                            DataSourceID="SqlDataSource1" 
                            DataTextField="Dept" 
                            DataValueField="Dept">
                        </asp:DropDownList>
                            <asp:DropDownList ID="DropDownList2" 
                            runat="server" 
                            AutoPostBack="True" 
                            CssClass="ddl" 
                            DataSourceID="SqlDataSource2" 
                            DataTextField="Group" 
                            DataValueField="Group">
                        </asp:DropDownList>
                        <asp:DropDownList   
                            ID="DropDownList3" 
                            runat="server" 
                            CssClass="ddl" 
                            DataSourceID="SqlDataSource3" 
                            DataTextField="EQUCODE" 
                            DataValueField="EQUCODE" 
                            SelectedIndex='<%# Bind("txt3") %>'>
                        </asp:DropDownList>

Open in new window


here are the 3 ddl datasources.
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MAS90ConnectionString %>" SelectCommand="SELECT distinct [Dept] FROM [aaa_v_Mpulse_EQU] order by [Dept]"></asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MAS90ConnectionString %>" SelectCommand="SELECT distinct [Group] FROM [aaa_v_Mpulse_EQU] WHERE ([Dept] = @Dept) order by [Group]">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="Dept" PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:MAS90ConnectionString %>" SelectCommand="SELECT [EQUCODE], [EQUDESC] FROM [aaa_v_Mpulse_EQU] WHERE (([Dept] = @Dept) AND ([Group] = @Group))">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="Dept" PropertyName="SelectedValue" Type="String" />
                    <asp:ControlParameter ControlID="DropDownList2" Name="Group" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>

Open in new window

0
Comment
Question by:sphilip951
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
I have a single dropdown in formview Insert and Edit and it works fine.
Where is txt3 coming from?
Can you start adding one ddl at a time and see what's breaking it.
0
 

Author Comment

by:sphilip951
Comment Utility
Formview1 uses SqlDataSource4.  txt3 is a field in the SqlDataSource4.

I removed the third dropdownlist and and the bind from the second and it works fine.  As soon as I add the bind to the 2nd dropdowlist it fails with the error above.

This works and the insert happens OK
                        <asp:DropDownList 
                            ID="DropDownList1" 
                            runat="server" 
                            AutoPostBack="True" 
                            CssClass="ddl" 
                            DataSourceID="SqlDataSource1" 
                            DataTextField="Dept" 
                            DataValueField="Dept"
                            selectedvalue='<%# Bind("txt3") %>'>
                        </asp:DropDownList>
                        <asp:DropDownList 
                            ID="DropDownList2" 
                            runat="server" 
                            DataSourceID="SqlDataSource2" 
                            DataTextField="Group" 
                            DataValueField="Group" 
                            >
                        </asp:DropDownList>

Open in new window


When I try to bind txt4 to the second dropdownlist it fails.
                        <asp:DropDownList 
                            ID="DropDownList1" 
                            runat="server" 
                            AutoPostBack="True" 
                            CssClass="ddl" 
                            DataSourceID="SqlDataSource1" 
                            DataTextField="Dept" 
                            DataValueField="Dept"
                            selectedvalue='<%# Bind("txt3") %>'>
                        </asp:DropDownList>
                        <asp:DropDownList 
                            ID="DropDownList2" 
                            runat="server" 
                            DataSourceID="SqlDataSource2" 
                            DataTextField="Group" 
                            DataValueField="Group" 
                            selectedValue='<%# Bind("txt4") %>'
                            >
                        </asp:DropDownList>

Open in new window

0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
Comment Utility
I am not sure then but why do you need to use Bind a FormView property to the dropdown?

Check these:
http://forums.asp.net/t/1032264.aspx
https://sites.google.com/site/aspdotnetprojects/home
0
 

Author Comment

by:sphilip951
Comment Utility
I removed the bind from all 3 dropdownlists, created 3 labels and made a postback event to update the labels.  I wish there was a way to do it all in aspx though without having to use the aspx.vb file.  If anyone has a way, please share.

    Protected Sub DropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        CType(FormView1.FindControl("lbl_dept"), Label).Text = CType(FormView1.FindControl("DropDownList1"), DropDownList).SelectedValue
        CType(FormView1.FindControl("lbl_group"), Label).Text = CType(FormView1.FindControl("DropDownList2"), DropDownList).SelectedValue
        CType(FormView1.FindControl("lbl_equip"), Label).Text = CType(FormView1.FindControl("DropDownList3"), DropDownList).SelectedValue
    End Sub

Open in new window

0
 

Author Closing Comment

by:sphilip951
Comment Utility
Thank you!!!  It's a bit more work than the label I came up with, but at least I know I'm not crazy.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now