Solved

ASPX Drop Down List Databind (Multiple DataSources)

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

680 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