Solved

ASP.net adding items to a DropDownList VB code

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IEnumerable<T> to a List<T> 8 63
Upgrade code from VS 2010 to VS 2015 7 45
asp.net repeater 2 32
Why am I losing my session variable value 11 33
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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