Solved

ASPX Drop Down List Databind (Multiple DataSources)

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 75
ASP.net Show "please wait " message as page loads 2 41
asp enable view state 1 24
Question about JQuery and asp.net 3 24
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…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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