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.
Related
In my ASP.NET Web Forms application, I'm using a DropDownList1 which displays the one column value and lets me choose another column value to bind to the control from the linked data source. I'm using the configuration wizard to select the settings, but it only allows me to display one column value, but I really need to display 2 columns and their values(i.e. first and last name). Would I be able to display multiple column values using the DropDownList control or do I have use something else?
The is what I have in my .aspx page.
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" DataTextField="LAST_NAME" DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:resourceConnectionString %>" SelectCommand="SELECT [ID], [LAST_NAME], [FIRST_NAME] FROM [EMPLOYEE]"></asp:SqlDataSource>
After thinking that there has to be an easier answer for what I wanted to accomplish, I was able to come up with a solution.
I made the data source something like "SELECT [ID], [FIRST_NAME], [LAST_NAME], [FIRST_NAME] + ' ' + [LAST_NAME] as NAME FROM [EMPLOYEE]" and then I made the DataTextField="NAME" display the first and the last name.
Here is my working code:
<asp:DropDownList ID="ddlEmployeeId" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourcePalcam" DataTextField="NAME" DataValueField="ID" OnSelectedIndexChanged="DropDownListEmployee_SelectedIndexChanged" Width="200px" Font-Size="Large">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourcePalcam" runat="server" ConnectionString="<%$ ConnectionStrings:resourceConnectionString %>" SelectCommand="SELECT [ID], [FIRST_NAME], [LAST_NAME], [FIRST_NAME] + ' ' + [LAST_NAME] as NAME FROM [EMPLOYEE] ORDER BY FIRST_NAME"></asp:SqlDataSource>
Is it possible to perform a SQL SELECT Statement that will select all rows, then output the rows to a label, which is created dynamically?
In the system I'm writing (pretty new to c#) I am performing a SQL SELECT from a view where a records ID can appear multiple times, but with different associated values, i.e.
ID - VALUE
1 - A
1 - B
1 - C
2 - A
2 - B
3 - A
What I want to do then is output each result to a its own label, but i would have no idea until the SELECT is ran how many labels I would need so I'd need code to draw them dynamically? Using the above examples A would return 3 labels, but B would only return 2.
If that makes any sense? E.G
foreach (result in sql)
{
label.Text = result
}
Thanks
You can use a repeater to do this.
https://msdn.microsoft.com/en-us/library/zzx23804(v=vs.85).aspx
<asp:Repeater ID="labelRepeater" runat="server"
DataSourceID="labelDataSource">
<ItemTemplate>
<asp:Label runat="server" ID="Label1"
text='<%# Eval("Value") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource
ConnectionString=
"<%$ _fill_in_your_string %>"
ID="labelDataSource" runat="server"
SelectCommand="SELECT ID, Value from myTable">
</asp:SqlDataSource>
Look here also: possible duplicate? How to add Repeater control dynamically from code behind?
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.
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.
}
}
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]"...