Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bind DropDownList

Status
Not open for further replies.

BasicBoy

Programmer
Feb 22, 2008
156
ZA
I am trying to bind a dropdownlist to a query like in :

cmdString = "SELECT username from users where isnull(username)=false and closed=false order by username"

cmdSelect = New OleDbCommand(cmdString, dbCnDiary)

dbCnDiary.Open()

ddlusers.DataSource = cmdSelect.ExecuteNonQuery
ddlusers.datatextfield = "Username"
ddlusers.DataBind()

dbCnDiary.Close()

I get a nullexception error on the executenonquery - what could that mean ?

Thanks

 
I have this code now and it gives me the error : Object Variable or with block not set which may be when I have not used the new keyword. However if I use New it tells me that I cannot as it will be a Friend (whatever that may mean - did not know that I had any friends)

Dim cmdStringUsers As String, cmdSelectUsers As OleDbCommand

cmdStringUsers = "SELECT userno, username from users where isnull(username)=false and closed=false order by username"

cmdSelectUsers = New OleDbCommand(cmdStringUsers, dbCnDiary)
dbCnDiary.Open()

drUsers=cmdSelectUsers.ExecuteReader())

If IsDBNull(drUsers) Then Exit Sub
ddlUsers.DataSource = drUsers
ddlUsers.DataTextField = "UserName"
ddlUsers.DataValueField = "UserNo"
ddlUsers.DataBind()

dbCnDiary.Close()

Help very much appreciated. The transfer from ASP.NET 1.1 to 4 is not very nice.
 
OK - I found the error
Nowhere am I told that I can only fill this ddl on a rowdatabound event and only when it is in edit mode.
 
I have no idea what you just said.
You can fill a drop down list on most events, you just have to do it when it makes most sense to for your situation.
 
I did not say that this ddl is embedded in a gridview template under and edititem entry - which it is. It seems that the ddl is not 'visible' to the application when it it is not in edit mode.
In any case - that is the only way I could make it work.
 
jbenson - I omitted to thank you for always attending to my questions. I appreciate.
 
I need to fill this ddl everytime a row goes into edit mode.

The ddl however can only be 'seen' by the program when that row is in datacontrolrowstate.edit mode

Presently I have it in the rowdatabound event, but that only fills the ddl in first row with items.

I have tried to place it under the rowediting event, but have been unable to succeed as the rowediting command binds the datagrid again.

I have tried to place it under the selectedindexchanged event, but then the row is not in edit mode.

Any suggestions welcome

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top