Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASPX Drop Down List Databind (Multiple DataSources)

Posted on 2013-01-25
5
Medium Priority
?
981 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 38820408
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
ID: 38820508
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 2000 total points
ID: 38820618
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
ID: 38820755
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
ID: 38824467
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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