DropDownList.DataSource = Nothing ???

M

Marc Robitaille

Hello,

I set the DataSource property of a DropDownList to as DataSet that is filled
from a SQLDataAdapter. The AutoPostBack property of that DropDownList is set
to True. When the SelectedIndexChanged occurs, if I set a BreakPoint in the
event and I check the values in the Watch window, I can see that the
DataSource property still contain the DataSet. When I set the DataSource of
a DropDownList to my object collection and check the values in the Watch
window when the SelectedIndexChanged occurs, the DataSource is set to
Nothing. Why? What appen to my object? Is my object need something so that
the DataSource property of the DropDownList can retain my object collection?
Hope that someone can help me.

Here are some parts of my code of my object collection. I have a VTBCodes
class that is a collection. I allso have VTBCode class that represent the
data. VTBCodes inherits from PAICollectionBase and implements iEntitys.
PAICollectionBase inherits from CollectionBase of the .net framework.
VTBCode inherits from EntityBase. EntityBase implements iEntity.

<Serializable()> Public MustInherit Class PAICollectionBase
Inherits CollectionBase
Implements IDisposable

Public MustOverride Function Add(ByVal pObject As Object) As Integer

.....

End Class

Public Interface iEntitys

Function GetData() As iEntitys
....

End Try

<Serializable()> Public Class VTBCodes
Inherits PAICollectionBase
Implements iEntitys

Public Overrides Function Add(ByVal pEntity As Object) As Integer
If TypeOf (pEntity) Is VTBCode Then
....
Else
Dim ex As New Exception("...
Throw ex
End If
....

Public Overloads Function GetData() As PAIiFactory.iEntitys Implements
PAIiFactory.iEntitys.GetData
Dim oconConnection As System.Data.SqlClient.SqlConnection
Dim odrVTBCode As System.Data.SqlClient.SqlDataReader = Nothing

oconConnection = Me.GetConnection

Dim strSQL As String = "Select...

odrVTBCode = SqlHelper.ExecuteReader(oconConnection, CommandType.Text,
strSQL)

While odrVTBCode.Read
oVTBCode = New VTBCode

oVTBCode.Desc.Value =
odrVTBCode.Item(oVTBCode.Desc.AliasName(False))

list.Add(oVTBCode)

End While

Return Me
....

End Class

Public Interface iEntity

Sub UpdateData(ByVal pConnection As
System.Data.SqlClient.SqlConnection, ByVal pTrans As
System.Data.SqlClient.SqlTransaction)

Sub DeleteData(ByVal pConnection As
System.Data.SqlClient.SqlConnection, ByVal pTrans As
System.Data.SqlClient.SqlTransaction)

Sub InsertData(ByVal pConnection As
System.Data.SqlClient.SqlConnection, ByVal pTrans As
System.Data.SqlClient.SqlTransaction)
....

End Interface

Public MustInherit Class EntityBase
Implements iEntity
Implements IDisposable

Public MustOverride Function GetData(ByVal pExpressionCollection As
iExpression) As iEntity Implements PAIiFactory.iEntity.GetData

Public MustOverride Overloads Sub UpdateData(ByVal pConnection As
System.Data.SqlClient.SqlConnection, ByVal pTrans As
System.Data.SqlClient.SqlTransaction) Implements
PAIiFactory.iEntity.UpdateData

Public MustOverride Overloads Sub DeleteData(ByVal pConnection As
System.Data.SqlClient.SqlConnection, ByVal pTrans As
System.Data.SqlClient.SqlTransaction) Implements
PAIiFactory.iEntity.DeleteData

Public MustOverride Overloads Sub InsertData(ByVal pConnection As
System.Data.SqlClient.SqlConnection, ByVal pTrans As
System.Data.SqlClient.SqlTransaction) Implements
PAIiFactory.iEntity.InsertData

....

End Class

<Serializable()> Public Class VTBCode
Inherits EntityBase

Private mfldDescFr As FieldString
....

Public Sub New()

mfldDesc = New FieldString("Desc", Me, "Desc", 100)'Name , Me,
AlisaName, Length
...

End Sub
....
Public Property Desc() As FieldString
Get
Return mfldDesc

End Get
Set(ByVal Value As FieldString)
mfldDesc = Value

End Set

End Property
....

Public Overrides Function GetData(ByVal pExpressionCollection As
iExpression) As iEntity
....

Public Overrides Function ToString() As String

'This will show the value in the DropDownList
Return Convert.ToString(mfldDesc.Value)

End Function
....

End Class

This is the code to fill the DropDownList

Dim oVTBCodes As New VTBCodes

cboCodeDivision.DataSource = oVTBCodes.GetData
cboCodeDivision.DataBind()
 
Z

zdrakec

"cboCodeDivision.DataSource = oVTBCodes.GetData
cboCodeDivision.DataBind() "

Be sure to set the DataText and DataValue properties of your drop-down:

cboCodeDivision.DataTextField = "FieldName"
cboCodeDivision.DataValueField = "FieldName"

Also, when the page reloads, its datasource will be nothing; so try
storing the dataset in the page cache if you need to retrieve it later:

Cache.Insert("keynamefordataset",dataset)

You can get it back:
dataset = ctype(Cache.Item("keynamefordataset"),dataset)

(I use "dataset" here for, really, any object).

Even though the datasource of your dropdown goes to Nothing, the text
and value properties you set at databinding time will still be
available:

cboCodeDivision.SelectedItem.Text and
cboCodeDivision.SelectedItem.Value

Hope this is helpful....
zdrakec
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top