Public Shared Function SelectDistinct(ByVal SourceTable As DataTable, ByVal ParamArray FieldNames() As String) As DataTable
Dim lastValues() As Object
Dim newTable As DataTable
If FieldNames Is Nothing OrElse FieldNames.Length = 0 Then
Throw New ArgumentNullException(”FieldNames”)
End If
lastValues = New Object(FieldNames.Length - 1) {}
newTable = New DataTable
For Each field As String In FieldNames
newTable.Columns.Add(field, SourceTable.Columns(field).DataType)
Next
For Each Row As DataRow In SourceTable.Select(”", String.Join(”, “, FieldNames))
If Not fieldValuesAreEqual(lastValues, Row, FieldNames) Then
newTable.Rows.Add(createRowClone(Row, newTable.NewRow(), FieldNames))
setLastValues(lastValues, Row, FieldNames)
End If
Next
Return newTable
End Function
=====================
Private Shared Function fieldValuesAreEqual(ByVal lastValues() As Object, ByVal currentRow As DataRow, ByVal fieldNames() As String) As Boolean
Dim areEqual As Boolean = True
For i As Integer = 0 To fieldNames.Length - 1
If lastValues(i) Is Nothing OrElse Not lastValues(i).Equals(currentRow(fieldNames(i))) Then
areEqual = False
Exit For
End If
Next
Return areEqual
End Function
========================
Private Shared Function createRowClone(ByVal sourceRow As DataRow, ByVal newRow As DataRow, ByVal fieldNames() As String) As DataRow
For Each field As String In fieldNames
newRow(field) = sourceRow(field)
Next
Return newRow
End Function
====================
Private Shared Sub setLastValues(ByVal lastValues() As Object, ByVal sourceRow As DataRow, ByVal fieldNames() As String)
For i As Integer = 0 To fieldNames.Length - 1
lastValues(i) = sourceRow(fieldNames(i))
Next
End Sub
Tuesday, May 27, 2008
filling the textbox,dropdownlist from the database
1 create table teamtable (teamcode int, teamname varchar(10))
2 insert into teamtable values(1,'teamA')
3 insert into teamtable values(2,'teamB')
4 insert into teamtable values(3,'teamC')
5
6 create table teammemtable(teamcode int, memcode int, memname varchar(50), memage int, memgndr char(1))
7 insert into teammemtable values(1,1,'dhana',27,'M')
8 insert into teammemtable values(1,2,'balan',27,'M')
9 insert into teammemtable values(1,3,'deepa',24,'F')
10
11 insert into teammemtable values(2,1,'balaji',26,'M')
12 insert into teammemtable values(2,2,'thunaivan',35,'M')
13 insert into teammemtable values(2,3,'geetha',28,'F')
14
15 insert into teammemtable values(3,1,'jibin',21,'M')
16 insert into teammemtable values(3,2,'rajan',27,'M')
17 insert into teammemtable values(3,3,'uma',26,'F')
aspx page
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="t-1208420.aspx.cs" Inherits="DropDownList_t_1208420" %>
2
3
4
5 6
7Untitled Page
8
9
10
47
48
2 insert into teamtable values(1,'teamA')
3 insert into teamtable values(2,'teamB')
4 insert into teamtable values(3,'teamC')
5
6 create table teammemtable(teamcode int, memcode int, memname varchar(50), memage int, memgndr char(1))
7 insert into teammemtable values(1,1,'dhana',27,'M')
8 insert into teammemtable values(1,2,'balan',27,'M')
9 insert into teammemtable values(1,3,'deepa',24,'F')
10
11 insert into teammemtable values(2,1,'balaji',26,'M')
12 insert into teammemtable values(2,2,'thunaivan',35,'M')
13 insert into teammemtable values(2,3,'geetha',28,'F')
14
15 insert into teammemtable values(3,1,'jibin',21,'M')
16 insert into teammemtable values(3,2,'rajan',27,'M')
17 insert into teammemtable values(3,3,'uma',26,'F')
aspx page
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="t-1208420.aspx.cs" Inherits="DropDownList_t_1208420" %>
2
3
4
5 6
7
8
9
10
47
48