user2210516
user2210516

Reputation: 683

Get selected value from DB and other hard coded values in dropdownlist asp.net

I have a gridview with a edit function. At gender column i have a dropdownlist that should give me two alternative Herr/Frau and also bind the selectedvalue correct.

At the Moment it works perfect as Long as i have both values presented in the db. What i mean is i have to have at least one customer already in db that is a male and one customer that is a female. If all my customers are males i only have the alternative "Herr" in my dropdownlist.

Can anyone explain how i can solve this Problem so i have the alternative to edit a Herr to a Frau without having any customers with a Frau value in db.

I have to make some changes in my select i think becuase at the Moment i only gather the distinct values of gender.

Here some code:

<asp:TemplateField HeaderText="Anrede" SortExpression="Gender">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="Gender" DataValueField="Gender" SelectedValue='<%# Bind("Gender") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EventConnectionString %>" SelectCommand="SELECT DISTINCT [Gender] FROM [Customer]"></asp:SqlDataSource>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
            </ItemTemplate>

Upvotes: 1

Views: 732

Answers (2)

Der U
Der U

Reputation: 3314

Replace

"SELECT DISTINCT [Gender] FROM [Customer]"

with

"SELECT 'Herr' [Gender] UNION ALL SELECT 'Frau' [Gender]"

this doesn't look for what's present in the source table. It hardcodes the desired items.

You want to add "Fräulein"? no problem. use

"SELECT 'Herr' [Gender] UNION ALL SELECT 'Frau' [Gender] UNION ALL SELECT 'Fräulein' [Gender]"

this is of course an ugly workaround. You should propably not populate Your dropdownlist by asking a fact-table at all. Asking a dimensional table, that holds such values as "Mr" and "Mrs" or "Herr" and "Frau" and orders them by regional standards seems more sound.

Upvotes: 1

OTTA
OTTA

Reputation: 1081

You populate the drop down from a seperate reference table that holds allowable values for such things as gender, title etc. This simplifies adding further options as required. Currently I work on Social Care case management systems, gender can be Male, Female, Unknown or Undefined.

Upvotes: 0

Related Questions