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>'
Related
I want to find the total number of active users who are members of the user table in my database. But I get an error like this:
DataBinding: 'System.Data.DataRowView' does not contain a property with name 'userid'
My code is as follows:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<h3><%#Eval("userid") %></h3>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT COUNT(*) FROM [user]"></asp:SqlDataSource>
The error means that there is no column named userid in the result from your query. And that makes sense since you are returning only one column. And you are not even naming that column with AS, so SQL makes it Expr1 as column name.
your query should be
SELECT COUNT(*) AS total_users FROM [user]
Now you can use the column name total_users in the Repeater
<h3><%#Eval("total_users") %></h3>
I want to populate a dropdown list with values from a table I created. I only want to populate the list with one of the fields- the languages in my table. I think I have connected to the data source correctly, but I don't know what I have to do to get the values into the list. I can enter my own values but I'd rather have this automated.
This is what I have so far, but I'm guessing there's more to it than just linking the list to the data source.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>"
SelectCommand="SELECT * FROM [Web_Metrics] WHERE ([LCID] = #LCID)">
<SelectParameters>
<asp:QueryStringParameter Name="LCID" QueryStringField="LCID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Select LCID: " ></asp:Label>
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="LCID" DataValueField="LCID">
<asp:ListItem>Select LCID...</asp:ListItem>
</asp:DropDownList>
Thanks for the help. I got the dropdown list populated now, but I was wondering how do I actually get the repeater I'm using to display the details of the LCID the person selects? I've seen people talking about page.isPostback but I don't know what that is or if it works with my current setup. I need to somehow get the LCID they selected and then refresh the page to show the details of that LCID. Does anyone have any ideas?
Your problem is that you're trying to define list items and a data source.
If you want to insert a "Select an item.." option, I would suggest prepending it to your resultset (getting it to always be first with a UNION and ORDER BY could be difficult depending on your fields) or inserting it after databinding in your code behind:
Modification to DropDownList1s attributes:
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="CountryName" DataValueField="LCID" OnDataBound="InsertChooseItem" />
C#:
protected void InsertChooseItem(object sender, EventArgs e)
{
ListItem selectOnePlease = new ListItem("Select LCID..", 0);
DropDownList1.Items.Insert(0, selectOnePlease);
}
VB:
Protected Sub InsertChooseItem(sender As Object, e As EventArgs)
Dim selectOnePlease As New ListItem("Select LCID..", 0)
DropDownList1.Items.Insert(0, selectOnePlease)
End Sub
You've specified the select parameter to be a query string, so the data in your DropDownList will only be populated when the URL resembles something like:
http://{Your server name}/Default.aspx?LCID=1
That doesn't make any sense though because the LCID column in your table should be unique, so although this will work there will only be one value in the drop down list.
I think what you want is to display all the languages from the database in the drop down, here's an example:
<asp:SqlDataSource ID="sqlDS" runat="server"
ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>"
SelectCommand="SELECT LCID,Language FROM [Web_Metrics]">
</asp:SqlDataSource>
<asp:DropDownList ID="ddlLanguages" AppendDataBoundItems="true" Width="150px" runat="server" DataSourceID="sqlDS" DataTextField="Language" DataValueField="LCID">
<asp:ListItem>Select Language</asp:ListItem>
</asp:DropDownList>
Just a note, you should never display the ID to the client, it's totally meaningless to them, the ids are mostly used by developers in the background that's why in the drop down I set the:
DataTextField="Language" (This is the language name visible to the user)
DataValueField="LCID" (Not visible to the user, but useful for any additional processing in code behind)
AppendDataBoundItems="true" - this line of code will keep all the items you've manually added to the drop down, e.g "Select Language" and will append any data bound items e.g from a SQL table
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
How can I display two columns in the DropDownList in asp.net?
SqlDataSourceNamePeop.SelectCommand = "select name,forename from people";
DropDownListCaptain.DataTextField = "name";
Like this it displays only the names. But how can i display the names+forenames?
<asp:DropDownList ID="DropDownListPeople"
runat="server"
DataSourceID="SqlDataSourceNamePeop">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceNamePeop"
ConnectionString="xxxxx"
runat="server">
</asp:SqlDataSource>
The easiest way is to make your sql query like this:
SqlDataSourceNameJuc.SelectCommand = "select name + ' ' +forename as FullName from people";
then bind this custom property to your drop down list like this:
DropDownListCaptain.DataTextField = "FullName";
Let me know if it fixed your problem, thanks.
Change your select command to
"Select name + ',' + forename as 'name' from people"
<asp:DropDownList ID="DropDownList9" runat="server"
DataSourceID="SqlDataSource3" DataTextField="**CALL_NAMESURNAME**"
DataValueField="CALL_NAME">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:McCarthyConnectionString %>"
SelectCommand="SELECT [CALL_NAME], [SURNAME],**CALL_NAME + SURNAME AS [CALL_NAMESURNAME]** FROM [Sheet1$] where COY_NAME LIKE '%abc%'">
</asp:SqlDataSource>
CHECK THE ** ** PARTS
IList<PM_ClientInformation> objPM_ClientInformation = new List<PM_ClientInformation>();
//get information in objPM_ClientInformation
foreach (PM_ClientInformation f in objPM_ClientInformation)
{
string text = string.Format("{0}{1}",
f.PM_cliFirstName.PadRight(10,'\u00A0'),
f.PM_cliLastName.PadRight(10,'\u00A0'));
ddlClientName.Items.Add(new ListItem (text ,f.PM_cliId.ToString()));
}
it defiantly work.
Dropdownlist cannot display two items. you have to merge trhem and then bind the result to it.
note that there are some other dropdownlist (ajax based or simple ) which are able to bind to two or more columns, but ASP.net is not able to cope with it
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";