Solved

ASP.net adding items to a DropDownList VB code

Posted on 2011-03-23
5
517 Views
Last Modified: 2012-06-22
In ASP.net I have a backend Access database.
On my WebForm, I have several DropDownLists.
I want to populate each of these with the unique items from
ecah column of Table1 in my backend database called GC.accdb.
What VB.net code would I use to do this?
I have learnt so far that the SQL code is "Select Distinct MyColumn From MyTable"
0
Comment
Question by:murbro
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35196786
I would suggest to retrieve all the data at once (the complete table myTable)

And then use a select distinc for each column that you want to bind to a combobox
http://www.thescarms.com/dotnet/BindCombobox.aspx
Dim OleDbConnection As System.Data.OleDb.OleDbConnection
Dim OleDbSelectCommand As System.Data.OleDb.OleDbCommand

OleDbSelectCommand.CommandText = "SELECT * FROM myTable"
OleDbSelectCommand.Connection = OleDbConnection

       
Dim myDataAdapter As System.Data.OleDb.OleDbDataAdapter
myDataAdapter = New System.Data.OleDb.OleDbDataAdapter()

myDataAdapter .SelectCommand = OleDbSelectCommand

Dim myDataSet As System.Data.DataSet
 
myDataAdapter.Fill(myDataSet )

DataTable dt=SelectDistinct("myTable", myDataSet .tables("myTable"),"MyColumn ")








private bool ColumnEqual(object A, object B)
{

if ( A == DBNull.Value && B == DBNull.Value ) 
return true; 
if ( A == DBNull.Value || B == DBNull.Value ) 
return false; 
return ( A.Equals(B) ); 

}


public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
{ 
DataTable dt = new DataTable(TableName);
dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);

object LastValue = null; 
foreach (DataRow dr in SourceTable.Select("", FieldName))
{
if ( LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])) ) 
{
LastValue = dr[FieldName]; 
dt.Rows.Add(new object[]{LastValue});
}
}
if (ds != null) 
ds.Tables.Add(dt);
return dt;
}

Open in new window

0
 

Author Comment

by:murbro
ID: 35196879
Hi.  Thanks. Where do I put that code. It is not VB code
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 35196930
Just in your class
(i just converted i through http://www.developerfusion.com/tools/convert/csharp-to-vb/, it's possible that there are some syntax errors)
Private Function ColumnEqual(A As Object, B As Object) As Boolean

	If A = DBNull.Value AndAlso B = DBNull.Value Then
		Return True
	End If
	If A = DBNull.Value OrElse B = DBNull.Value Then
		Return False
	End If
	Return (A.Equals(B))

End Function


Public Function SelectDistinct(TableName As String, SourceTable As DataTable, FieldName As String) As DataTable
	Dim dt As New DataTable(TableName)
	dt.Columns.Add(FieldName, SourceTable.Columns(FieldName).DataType)

	Dim LastValue As Object = Nothing
	For Each dr As DataRow In SourceTable.[Select]("", FieldName)
		If LastValue Is Nothing OrElse Not (ColumnEqual(LastValue, dr(FieldName))) Then
			LastValue = dr(FieldName)
			dt.Rows.Add(New Object() {LastValue})
		End If
	Next
	If ds IsNot Nothing Then
		ds.Tables.Add(dt)
	End If
	Return dt
End Function

Open in new window

0
 

Author Comment

by:murbro
ID: 35197076
I am lost...don't understand this
0
 

Author Closing Comment

by:murbro
ID: 35202017
thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP:Radiobuttonlist/asp:RadioButtonListItem custom styling 1 42
convert html to string 6 34
Output Caching in IIS 2 30
Reading the Web.Config using IIS 7.5? 4 36
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question