Adding Value to databound DropDownlist (asp.net) - asp.net

I am creating a website which has 1 dropdownlist that bound to Northwind database, Customers table. This Drop downlist will list all the countries from the tables. I have tried to add "Japan" (which was not in the list) to this dropdownlist. It was added but it always appear at the top (or default value). My question is: Is it possible to add Japan and not make it appear at the top, so it will follow the alphabetical order?
This is my code:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Country"
DataValueField="Country"><asp:ListItem Text ="Japan" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>"
SelectCommand="SELECT Country FROM Customers GROUP BY Country ORDER BY Country">
</asp:SqlDataSource>

You can try this way in your sql script.
SELECT Country
FROM
(
SELECT Country
FROM Customers
GROUP BY Country
UNION ALL
SELECT 'JAPAN'
) M
ORDER BY Country

Related

Using a different value from a drop down list

Sorry my title is crap but cant think of a better way to put it.
Using MS VS 2013 and MS SQL server 2012. VB
I have a drop down list and the stored procedure it calls gets two values from a table in SQL. The values are the ID and the name. The drop down list is displaying the name but I want to use the ID when a name is selected in the drop down list in code.
How do I do this?
My code for the DDL is
<asp:DropDownList ID="DDLAllTreatments" runat="server" DataSourceID="AllTreatments" DataTextField="Name" DataValueField="Name"></asp:DropDownList>
<asp:SqlDataSource ID="AllTreatments" runat="server" ConnectionString="<%$ ConnectionStrings:InfinitySPa %>" SelectCommand="SP_GetAllTreatments" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Change DataValueField to ID:
<asp:DropDownList ID="DDLAllTreatments" runat="server" DataSourceID="AllTreatments" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
<asp:SqlDataSource ID="AllTreatments" runat="server" ConnectionString="<%$ ConnectionStrings:InfinitySPa %>" SelectCommand="SP_GetAllTreatments" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

SQL insert always take dropdownlist default value

I have two asp dropdown lists,the second one will generate options when the first dropdown list has a selected value. Both dropdown list are getting data from database.
However, when I try to insert these two values into my table,the first dropdown list item will insert correctly,the second dropdown list always inserts the first item into my table.
After some testing, I found out that the second dropdown list is not selected and the first item was inserted into my table because its the default value.
BTW, my first dropdown list is autopostback while the second one is not.
I am using Microsoft Web Developer 2010 express and I am using Visual Basic.
first dropdown list:
<asp:DropDownList ID="stockcodeddl" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="stockcode"
DataValueField="stockcode" Width="174px" Height="19px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:oakwell %>"
SelectCommand="SELECT [stockcode] FROM [orders] GROUP BY [stockcode]">
</asp:SqlDataSource>
second dropdown list:
<asp:DropDownList ID="componentddl" runat="server" DataSourceID="ComponentsByStockCode" DataTextField="component"
DataValueField="stockcode" Width="174px" Height="19px"
Enable="true" onchange="getComponent()" >
</asp:DropDownList>
<asp:TextBox ID="comtxt" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="ComponentsByStockCode" runat="server"
ConnectionString="<%$ ConnectionStrings:oakwell %>"
SelectCommand="SELECT component, stockcode FROM orders WHERE ([stockcode] = #stockcode) GROUP BY stockcode, component">
<SelectParameters>
<asp:ControlParameter ControlID="stockcodeddl" Name="stockcode" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Please check that your dropdown binding data from the database was within the IsPostback on Page init. If not then before button event fires it will rebind the dropdown on page init function.
Try
dropdown2.selectedvalue
or
dropdown2.selecteditem

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>
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.
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.

Grid View: getting one column value from other table

I made form for inserting values into DB, and one column is CompanyID, and there is table Companies where that CompanyID is primary key.
How to get like dropdown menu or something, so users don't type like 1213 (ID), but they select it from list, or something like that?
Thanks.
Following is the simplest method.
Create & configure a SqlDataSource.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [CompanyID], [CompanyName] FROM [tbl_Company] ORDER BY [CompanyName]">
</asp:SqlDataSource>
Drag and drop a dropdown list from the toolbox. set datasourceid, datatext field and data value field.
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="CompanyName" DataValueField = "CompanyID"
DataMember="DefaultView" DataSourceID="SqlDataSource1">
</asp:DropDownList>
Run the page.
Select company name and ID from company table in array.
then with that array create your drop down list as follows
SQl = "select id, name from company";
echo '<select>'
foreach($companies as $company)
echo "<option value='$company[id]'>$company['name']</option>";
echo '</select>'

How to do a data lookup field in a DetailsView?

I have a master-detail page for Customers. Select a customer from the list and a details view opens with Name, Address...etc.
I've been asked to add a field listing the Sales Rep servicing that customer.
I want the new field to hold the foreign key to the SalesRep table: the SalesRepID. An integer.
I am not sure how to "wire up" the ItemTemplate field for displaying the Sales Rep name in a label. Also not sure about the EditItemTemplate dropdown list of possible Sales Reps.
I know I need to create a datasource to retreive the Sales Reps into a dataSet.
Let's call it "SQL_Reps_source".
A DropDownList seems to only like a list of values, and doesn't seem to handle Keys and Values like a HashTable or SortedList would.
Any advice on how to go about this?
Thanks
The following example adds a DropDownList to the EditItemTemplate of the DetailsView. The DataSourceID is set to the data source control that retrieves the sales reps names and id's.
<asp:TemplateField HeaderText="Sales Rep">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT SalesRepID, SalesRepName FROM SalesReps">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownList" Runat="server"
DataTextField="SalesRepName" DataValueField="SalesRepID"
SelectedValue='<%# Bind("SalesRepID") %>'
DataSourceID="SqlDataSource">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label" Runat="server"
Text='<%# Bind("SalesRepName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
For display purposes - return the NAME of the Sales Rep. rather than ID
DropDownList (ddl) handles both Values and Keys just fine, the naming is a bit off though. ddl.DataTextField property will hold the Name, ddl.DataValueField property will hold the ID. (see this answer for more details on this)
In the EditTemplate you'd need to fill the DropDown as described above, and then set its SelectedValue to the ID of Sales Rep. in quesion.
Hope this helps.
When you bind the dropdownlist do this:
ddl.DataSource = SQL_Reps_source;
ddl..DataTextField = "fullname";
ddl..DataValueField = "id";

Resources