DropDownList

R

Rick

I have been trying to get this dropdownlist to run and I am having nothing
but errors. Anyone know what I am doing wrong?

Any help would be much appreciated!!!

Here is my code.

<script runat="server">
Public Sub Page_Load(sender as Object, e as EventArgs)
Dim conClsf As OleDbConnection
Dim cmdMbrs As OleDbCommand
Dim rdrMbrs As OleDbDataReader
Dim State as String
conClsf = New OleDbConnection( _
"Provider=SQLOLEDB;" & _
"Server=192.168.1.3,2284;" & _
"Database=DataBase;" & _
"Uid=UserID;" & _
"Pwd=xxxxxxx" )
conClsf.Open

cmdMbrs = New OleDbCommand( _
" select DISTINCT State, FullState from jH where state = '" &
state & "'", _
conClsf)

state.DataSource = cmdMbrs.ExecuteReader()
state.DataTextField = "FullState"
state.DataValueField = "State"
state.DataBind()
state.DataSource.Close

End Sub
</script>

HTML stuff......

<asp:DropDownList DataTextField="FullState" DataValueField="State" ID=state
runat="server" />
 
G

Gen

Hello Rick,

If you provide the error message itself it might be easier to solve your
problem, but there is at least one mistake in you source code:

state.DataSource doesn't expose a method Close(). You should either save the
datareader to a local variable and call its Close() method or typecast
state.DataSource to OleDbDataReader and then call the Close() method.

And one more thing - you should close the connection to the database too -
otherwise you may end up leaking connections to the database
 
S

Steven Cheng[MSFT]

Hi Rick,

I agree with Gen and here is a simple workable page code which use code to
populate a dropdownlist by databinding. Also, if you meet any more specific
issue, you paste the error message or complete exception content so that we
can have a reference:


========================================
Partial Class databind_VBDropDownPage
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not IsPostBack Then

BindList()

End If

End Sub

Protected Sub BindList()

Dim reader As IDataReader

reader = "Get Data Reader from Command ............"


DropDownList1.DataSource = reader
DropDownList1.DataTextField = "CategoryName"
DropDownList1.DataValueField = "CategoryID"

DropDownList1.DataBind()

reader.Close()


End Sub
End Class

===========================================


BTW, I've also found your another thread "If Statement in HTML" in the
newsgroup and have posted a reply there with some test code. I'd appreicate
if you also have a look there.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rick

Sorry I meant to include this.

Compiler Error Message: BC30456: 'DataSource' is not a member of 'String'.
 
R

Rick

Do you mean like this

rdrMbrs = cmdMbrs.ExecuteReader
and then this
rdrMbrs.Close()

Something like that?
 
G

Gen

Yes, this will do fine:
rdrMbrs = cmdMbrs.ExecuteReader
// sourcecode
rdrMbrs.Close()

And about the error - Compiler Error Message: BC30456: 'DataSource' is not a
member of 'String'.

The problem is that you have declared a local variable of type string with
the name State:
Dim State as String

You have also given the dropdownlist the same ID as the variable above -
state
<asp:DropDownList DataTextField="FullState" DataValueField="State" ID=state
runat="server" />

As far as I know Visual Basic is not case sensitive, so you find yourself in
a duplicate names scenario (to say nothing of the fact you don't initialize
the State variable).

I suggest:
1 - eliminate the duplicate names
2 - initialize the State variable
3 - finally close the connection - conClsf.Close();
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top