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
Solved

ASP.net adding items to a DropDownList VB code

Posted on 2011-03-23
5
518 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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