Jeffrey Van Laethem
Jeffrey Van Laethem

Reputation: 2651

ASP.NET DropDownList in FormView

Been working on an ASP.NET form, and struggling with one particular part. I'm using a SqlDataSource with an Insert function that points to a stored proc, with a few input parameters. I have a FormView set up. I would like for one of the form's input fields to be a DropDownList whose options are populated from a different SqlDataSource. For reference, here is the formview:

<asp:FormView runat="server" id="FormView1" DataSourceID="SqlDataSource1" DefaultMode="Insert">
    <EditItemTemplate>
        refund_ach:
        <asp:CheckBox Checked='<%# Bind("ach") %>' runat="server" id="refund_achCheckBox" />
        <br />
        refund_dba_name:
        <asp:TextBox Text='<%# Bind("dbaname") %>' runat="server" id="refund_dba_nameTextBox" />
        <br />
        refund_mid:
        <asp:TextBox Text='<%# Bind("mid") %>' runat="server" id="refund_midTextBox" />
        <br />
        refund_description:
        <asp:TextBox Text='<%# Bind("description") %>' runat="server" id="refund_descriptionTextBox" />
        <br />
        refund_amount:
        <asp:TextBox Text='<%# Bind("amount") %>' runat="server" id="refund_amountTextBox" />
        <br />
        refund_type:
        <asp:TextBox Text='<%# Bind("type") %>' runat="server" id="refund_typeTextBox" />
        <br />
        <asp:LinkButton runat="server" Text="Update" CommandName="Update" id="UpdateButton" CausesValidation="True" />&nbsp;
        <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" id="UpdateCancelButton" CausesValidation="False" />
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:Table runat="server" id="Table1">
            <asp:TableRow>
                <asp:TableCell>ACH?:
                </asp:TableCell>
                <asp:TableCell><asp:CheckBox Checked='<%# Bind("ach") %>' runat="server" id="refund_achCheckBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>DBA Name:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("dbaname") %>' runat="server" id="refund_dba_nameTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>MID:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("mid") %>' runat="server" id="refund_midTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Description:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("description") %>' runat="server" id="refund_descriptionTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Amount:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("amount") %>' runat="server" id="refund_amountTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Type:
                </asp:TableCell>
                <asp:TableCell><asp:DropDownList DataSourceID="SqlDataSource2" SelectedValue='<%# Bind("type") %>' DataTextField="Reason" DataValueField="ID" runat="server" id="refund_typeTextBox" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
                    <asp:LinkButton runat="server" Text="Insert" CommandName="Insert" id="InsertButton" CausesValidation="True" />&nbsp;
        <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" id="InsertCancelButton" CausesValidation="False" />
    </InsertItemTemplate>
    <ItemTemplate>
        refund_ach:
        <asp:CheckBox Checked='<%# Bind("ach") %>' runat="server" id="refund_achCheckBox" Enabled="false" />
        <br />
        refund_dba_name:
        <asp:Label Text='<%# Bind("dbaname") %>' runat="server" id="refund_dba_nameLabel" />
        <br />
        refund_mid:
        <asp:Label Text='<%# Bind("mid") %>' runat="server" id="refund_midLabel" />
        <br />
        refund_description:
        <asp:Label Text='<%# Bind("description") %>' runat="server" id="refund_descriptionLabel" />
        <br />
        refund_amount:
        <asp:Label Text='<%# Bind("amount") %>' runat="server" id="refund_amountLabel" />
        <br />
        refund_type:
        <asp:Label Text='<%# Bind("type") %>' runat="server" id="refund_typeLabel" />
        <br />
    </ItemTemplate>
</asp:FormView>

And here is the SqlDataSource:

    <asp:SqlDataSource runat="server" id="SqlDataSource1" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=myserver;Initial Catalog=mydatabase;Persist Security Info=True;User ID=mysvcaccount;Password=NOPE!;" SelectCommand="SELECT [refund_ach], [refund_dba_name], [refund_mid], [refund_description], [refund_amount], [refund_type] FROM [refund]" InsertCommand="EXEC usp_refund_insert @user, @ach, @dbaname, @mid, @description, @amount, @type">
    <InsertParameters>
        <asp:formparameter FormField="refund_achCheckBox" Name="ach" Type="Boolean" />
        <asp:formparameter FormField="refund_amountTextBox" Name="amount" Type="Decimal" />
        <asp:formparameter FormField="refund_dba_nameTextBox" Name="dbaname" Type="String" />
        <asp:formparameter FormField="refund_descriptionTextBox" Name="description" Type="String" />
        <asp:formparameter FormField="refund_midTextBox" Name="mid" Type="String" />
        <asp:formparameter FormField="refund_typeTextBox" Name="type" Type="Int32" />
        <asp:parameter DefaultValue="dfsdf" Name="user" Type="String" />
    </InsertParameters>
</asp:SqlDataSource>

The data populates in the DDL just fine (from another SqlDataSource). All of the desired options come up. But when submitting the form, I get an error. I removed the DDL and hard-coded a value to that parameter, and it executed fine. I spent the last couple hours scouring the internet for a solution, finding tons, trying them, and not having any luck. I'm a DBA by trade, so .NET is not my forté, but it really doesn't seem like it should be this difficult to get a DDL to work like this.

Upvotes: 1

Views: 784

Answers (1)

Jeffrey Van Laethem
Jeffrey Van Laethem

Reputation: 2651

WOW. After being very frustrated, I decided to start from scratch and give it another go, just to make sure I didn't miss anything. I built everything EXACTLY the same, MINUS all of the table elements... and it worked perfectly. I added the table elements back, stopped working. Removed them again: works.

No idea why, perhaps an .NET-proficient person can explain why, but it definitely solved my problem.

Upvotes: 1

Related Questions