Live Chat Icon For mobile
Live Chat Icon

How to filter distinct records from a normal Select query and display in a web control

Platform: ASP.NET| Category: ADO.NET

‘Select distinct <field> from <table>’.This SELECT statement is used to filter out duplicate results from a query’s output. But sometimes the requirement is to use a Stored Procedure which returns the entire data and display the manipulated distinct data in web server control.
To do this use SortedList

For using SortedList import namespace System.Collections

Stored Procedure


Create Procedure GetSuppliers AS

SELECT  * FROM Suppliers
GO

<asp:DataList id='DataList1' runat='server'>
	<ItemTemplate>
		<%#Container.DataItem%>
	</ItemTemplate>
</asp:DataList>

VB.NET


Dim ds As New DataSet
Dim myda As SqlDataAdapter = New SqlDataAdapter('GetSuppliers', 'server=localhost;database=Northwind;uid=sa;pwd=;')
myda.SelectCommand.CommandType = CommandType.StoredProcedure
myda.Fill(ds, 'Table')
Dim slist As SortedList = New SortedList
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
	If Not slist.ContainsValue(dr('Country')) Then
	                slist.Add(dr('Supplierid'), dr('Country'))
	End If
Next
DataList1.DataSource = slist.GetValueList
DataList1.DataBind()
’In case of Dropdownlist
’DropDownList1.DataSource = slist.GetValueList
’DropDownList1.DataBind()

C#


DataSet ds = new  DataSet();
SqlDataAdapter myda    = new SqlDataAdapter('GetSuppliers', 'server=localhost;database=Northwind;uid=sa;pwd=;');
myda.SelectCommand.CommandType = CommandType.StoredProcedure;
myda.Fill(ds, 'Table');
SortedList slist    = new SortedList();
 
foreach( DataRow dr in ds.Tables[0].Rows)
{
	if (! slist.ContainsValue(dr['Country']))  
	{
		slist.Add(dr['Supplierid'].ToString (), dr['Country'].ToString ());
	}
} 
DataList1.DataSource = slist.GetValueList();
DataList1.DataBind();
//In case of DropDownList
//DropDownList1.DataSource = slist.GetValueList
//DropDownList1.DataBind()

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.