Link to home
Start Free TrialLog in
Avatar of Edward Diaz
Edward DiazFlag for United States of America

asked on

problem with Dropdownlist and selecting correct item from database (VB.NET)

I have a datagrid with a details button. After I click on the button, it puts the data from the datagrid that needs to be edited in a series of databound textboxes. That works fine. Now what I need is to get a value in the datagrid to match the values from a second table where the same values reside in a different table than the datagrid using a dropdownlist. I can print the selectedindex from the non-datgrid table and match it up with the datagrid for the same record, but I have NO IDEA how to get the selected value in the dropdownlist so it's selected and toggles to whatever the value in the record may be. The dropdownlist only points to the first option (of the non-datagrid table) like it's not hooked up to the datagrid table. I don't have anything in the page_load event if that makes any difference.

Here is my code (VB.NET):

'---> OLEDBconnection and OLEDBcommand declared
Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("\db\prototype.mdb")
Dim oleComm AS NEW OleDb.oleDbCommand()
oleComm.Connection=oleConn

'---->Hospital is the table of non-datagrid values to fill the dropdownbox
oleComm.CommandText="SELECT * FROM Hospital"
dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()
da.selectCommand=Olecomm
da.Fill(ds,"Hospital")
Hosp1.datasource=ds.Tables("Hospital").DefaultView

'------> Hosp1 is my dropdownbox control ID
'----> Hosp is the value of one of the textboxes (data from datagrid) displaying the data to matchup with the dropdownlist (options from non-datagrid table)
Hosp1.Selectedindex = Hosp1.Items.IndexOf(Hosp1.Items.FindByValue(Hosp.text))

'-----> The following is my verification that I can get the selecteditem and selectedindex from the non-datagrid table
dim first as string="The hospital in spot on the dropdown is "
dim last as string =" and hospital "
response.write(first)
response.write(Hosp1.selectedindex)
response.write(" ")
response.write(Hosp1.selecteditem)
response.write(last)
response.write(Hosp.text)
oleConn.Close()
hosp1.databind()
Avatar of mufaddal
mufaddal

Hi

you can use

"hosp1.selecteditem.text" for getting TEXTwhich is visible in dropdownlist

"hosp1.selecteditem.value" for getting internal VALUE connected with dropdwonlist TEXT


Hosp1.datasource=ds.Tables("Hospital").DefaultView
Hosp1.datatextfield=ds.Tables("Hospital").columns("textcolumnname")
Hosp1.datatextfield=ds.Tables("Hospital").columns("valuecolumnname")
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial