How to assign values to items in a databound ddl? - c#

I'm running a dropdown list populated by column "CustName" from table "Customer", and each customer has a unique id I want to use to run the queries; obviously it's best to have the end user select by name rather than id, because it's friendlier that way. How do I assign values to items in this databound ddl? Preferably, I'd like to assign their CustId values from the table itself. I tried simply placing the CustId field into the DataValueField attribute, but no go. Ideas?
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlCustNameSource" DataTextField="CustName"
DataValueField="CustName" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="Choose Customer" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlCustNameSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [CustName] FROM [Customer]"></asp:SqlDataSource>

What exactly does "No Go" mean?
...DataValueField="CustId"...
...SelectCommand="SELECT [CustName], [CustId] FROM [Customer]"...

Related

How to make a Bind in SqlDataSource - Dropdownlist

This is how I should be able to update a product on the page, it must grasp the category which I have chosen, so I will be free to save my choice every time.
I chose to do it via SqlDataSource dropdownlist.
I have written the code like this:
<asp:DropDownList ID="DropDownList1"
runat="server"
CssClass="form-control"
DataSourceID="SqlDataSource1"
DataTextField="navn"
DataValueField="Id"
SelectedValue='<%# Bind("kategori") %>'
></asp:DropDownList>
<%--datasource--%>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'
SelectCommand="SELECT kategori.id, kategori.navn, produkter.fk_kategori FROM kategori INNER JOIN produkter on kategori.id=produkter.fk_kategori ORDER BY [Id] DESC"></asp:SqlDataSource>
It appears this one error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
<asp:DropDownList ID="DropDownList1"
What should I do to the grasp it I've chosen category.

How to set initial value for dropdownlist while using sqldatasource as binding

I have 2 dropdownlist, which are bound to a SQLDataSource and value of 2nd dropdownlist is based on 1st dropdownlist. The problem is that by default dropdownlist are displaying actual values. I put initialvalue in dpsem so, it's working correctly.but when i do same in dpsubj dropdownlist it is showing previous value also.
If I select dpsem--4 then dpsubj should be A,B,C, and if if i select dpsem--6 then dpsubj should be D,E,F. But it is displaying A,B,C,D,E,F...
I am knowing that it is problem related to Autopostback...
<asp:DropDownList ID="dpsem" runat="server" Height="28px"
Width="99px" AutoPostBack="True" DataSourceID="selectsemester"
DataTextField="sem_no" DataValueField="sem_no" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Value="0">-select</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dpsubj" runat="server" Height="28px"
Width="99px" AutoPostBack="True" DataSourceID="Selectscode"
DataTextField="scode" DataValueField="scode" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Value="0">-select</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="Selectscode" runat="server"
ConnectionString="<%$ ConnectionStrings:AttendenceManagementConnectionString %>"
SelectCommand="SELECT DISTINCT [scode] FROM [Semester_Wise_Subject] WHERE (([branch_name] = #branch_name) AND ([sem_no] = #sem_no))">
<SelectParameters>
<asp:ControlParameter ControlID="lbdept" DefaultValue="IT" Name="branch_name"
PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="dpsem" DefaultValue="6" Name="sem_no"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="selectsemester" runat="server"
ConnectionString="<%$ ConnectionStrings:AttendenceManagementConnectionString %>"
SelectCommand="SELECT DISTINCT [sem_no] FROM [Batch_year]">
</asp:SqlDataSource>
You should use dpsubj.Items.Clear().. and then again load the filtered data on selectedindexchanged it will first delete all teh previous records in dpsubj dropdown and then use code to load value in dpsubj
I believe you have to create a SelectedIndexChanged event on the first drop down and have that event cause a databind on the second drop down. Also by default if you don't want the second drop down to display data get rid of the default parameter values.
If you are doing anything with ajax you also want to make sure that the first drop down updates the second in your script manager as well.
set AppendDataBoundItems="false" on dpsubj
since you set this property as true, items are not cleared before data binding.

Add ALL in Bound DropDownList

I currently developing a webpage using C#. I have a DropdownList data bound with the data from my sql database. After the dropdownlist bind with my database, the item inside the dropdownlist are userA, userB, and userC. If i select any of the item inside the dropdownlist, the data of the particular user will show in the gridview.
So, what I am trying to do now is I want to add an ALL into the dropdownlist. When I click on the ALL, the data of each user will be shown in the gridview. How can I achieve that? Any advice? Thanks.
P/S: I dont want to add any extra button in order to show all the user data. I want to make it in the dropdownlist.
This is my code:
WebApp.aspx:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
This is what i have edited for a new Webpage. I do not call the data binding function in code behind.
Thanks for any helps.
This is the answer i am looking for:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username" AppendDataBoundItems="True">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
Thanks everyone who is trying to help me.
You can achieve this:
Use this code after you bind the DropdownList with SQL data
ddlUsers.Items.Add(new ListItem("All", "all"));
ddlUsers.SelectedValue = "all";
Once this is done, you can make your select all user query based on this condition:
if(ddlUsers.SelectedValue == "all")
{
// your SQL query to select all users goes here.
}
In HTML Markup you can add this like:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
You can remove Selected="True" if you don't want this to be selected by default.
You can do few things to achieve this.
1)ddlUsers.Items.Add(new ListItem("All", "all")); add this line where ever you are binding the drop-down list.
2) use SelectedIndexChanged event of drop down like...
protected void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlUsers.SelectedValue == "all")
{
//Call your SQL query from here and bind the result set with your grid.
//if you need the id's of all the items in the drop down then write a loop and form a //string with , separated valued and pass it along.
}
}

ASP.NET Dropdownlist Issue duplicates

I have a drop down list that pulls a list of last names. The problem is some people have the same last name and for some reason it always shows one name. The drop down list is connected to a sql server and here is the code for the drop down list:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource3" DataTextField="lName" DataValueField="lName"
AppendDataBoundItems="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">Select</asp:ListItem>
</asp:DropDownList>
and here is the SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>"
SelectCommand="SELECT DISTINCT [lName] FROM [CoaTest]"></asp:SqlDataSource>
Thank you in advance!
You're using SELECT DISTINCT in your sql. When using DISTINCT, it will load all of the results from your query, and then select the distinct values. So in your case, if you have these last names returned as your result set
Smith
Markson
Smith
Henson
Smith
Henson
It will select the distinct values from that, and ultimately return
Smith
Markson
Henson
Your sql query is selecting DISTINCT values.
SELECT DISTINCT [lName] FROM [CoaTest]
If you want all values returned then remove the DISTINCT keyword.

Dropdownlist in Detailsview, not listing the items

Hello Everyone
I have a detailsview, where there are 3 bound fields and a template field.
The template field has a DropDownList, which I have connected to an AccessDataSource.
But when I run, the dropdownlist has just the "System.Data.DataRowView" as it's items.
I wish to get the items from DB to be listed down in DropDownList
This is my code
asp:TemplateField HeaderText="State/Province" SortExpression="State/Province">
<EditItemTemplate>
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="AccessDataSource1"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/db1.mdb"
SelectCommand="SELECT [State/Province_name] FROM [State/Province_List ]">
</asp:AccessDataSource>
</EditItemTemplate>
</asp:TemplateField>
Should I add "DataBinding" or "DataBound" event for DropDownList?? to make it perfect?
Help me regarding this issue
Thanks,
Arjun
Define the filed to use in the drop down list
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="State/Province_name"
DataValueField="State/Province_name"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
If you have problems with this non-standard column name, try to use an alias
SELECT [State/Province_name] AS StateProv FROM [State/Province_List ]
(Is the space in [State/Province_List ] OK?)
Then use this one
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="StateProv"
DataValueField="StateProv"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

Categories

Resources