Solved

ASP.net adding items to a DropDownList VB code

Posted on 2011-03-23
5
522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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