Solved

ASPX Drop Down List Databind (Multiple DataSources)

Posted on 2013-01-25
5
881 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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

13 Experts available now in Live!

Get 1:1 Help Now