Edward Diaz
asked on
ASP.NET DropDown List using the DataAdapter instead of a DataReader and finding the Selected index of data from Access Database
Hello!
Ok, here's my problem:
I have two tables in the same database, for this example I'll call them table1 and table2. Table1 has all of the information in it and table2 is used only as a list to provide a value for a field in table1. I can create a dropdownlist showing all of the values in table2, but cannot get it to show the selected index of table1 for the value that's selected. Also, I can get the value that's selected in table1, but can't get the rest of the dropdown list (all the values in table2 EXCEPT the one that's selected) to show up. I am using the DataAdapter instead of the DataReader (which nearly all of the examples I've seen use) to retrieve the values in the list because I need to eventually be able to modify the list itself. Any ideas??
Here is my code for what I have (2 examples):
'------------------------- ---THIS CODE ONLY WILL SHOW ALL OF THE LIST FROM TABLE2 BUT NOT THE SELECTED INDEX FROM TABLE1------------
Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString=" Provider=M icrosoft.J et.OLEDB.4 .0;data source=" & server.mappath("\db\protot ype.mdb")
Dim oleComm AS NEW OleDb.oleDbCommand()
oleComm.Connection=oleConn
oleComm.CommandText="SELEC T * FROM table2"
dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()
da.selectCommand=Olecomm
da.Fill(ds,"Table2")
'---Hosp1 is the dropdownlist control
Hosp1.datasource=ds.Tables ("Table2") .DefaultVi ew
oleConn.Close()
hosp1.databind()
'------------------------E ND EXAMPLE 1------------------------- ---------- ------
'------------------------- ---THIS CODE ONLY WILL SHOW THE CORRECT SELECTED INDEX FROM TABLE1 , BUT NOT THE REST OF THE LIST FROM TABLE2------------
Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString=" Provider=M icrosoft.J et.OLEDB.4 .0;data source=" & server.mappath("\db\protot ype.mdb")
Dim oleComm AS NEW OleDb.oleDbCommand()
oleComm.Connection=oleConn
'hosp.text is a value in table1 equal to value HospCode in Table2
oleComm.CommandText="SELEC T * FROM Table2 WHERE Hospcode=" + Hosp.text
dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()
da.selectCommand=Olecomm
da.Fill(ds,"table2")
'---Hosp1 is the dropdownlist control
Hosp1.datasource=ds.Tables ("table2") .DefaultVi ew
oleConn.Close()
hosp1.databind()
'-----------------END EXAMPLE 2------------------------- -----
Thank you in advance!
Kittrick
Ok, here's my problem:
I have two tables in the same database, for this example I'll call them table1 and table2. Table1 has all of the information in it and table2 is used only as a list to provide a value for a field in table1. I can create a dropdownlist showing all of the values in table2, but cannot get it to show the selected index of table1 for the value that's selected. Also, I can get the value that's selected in table1, but can't get the rest of the dropdown list (all the values in table2 EXCEPT the one that's selected) to show up. I am using the DataAdapter instead of the DataReader (which nearly all of the examples I've seen use) to retrieve the values in the list because I need to eventually be able to modify the list itself. Any ideas??
Here is my code for what I have (2 examples):
'-------------------------
Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString="
Dim oleComm AS NEW OleDb.oleDbCommand()
oleComm.Connection=oleConn
oleComm.CommandText="SELEC
dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()
da.selectCommand=Olecomm
da.Fill(ds,"Table2")
'---Hosp1 is the dropdownlist control
Hosp1.datasource=ds.Tables
oleConn.Close()
hosp1.databind()
'------------------------E
'-------------------------
Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString="
Dim oleComm AS NEW OleDb.oleDbCommand()
oleComm.Connection=oleConn
'hosp.text is a value in table1 equal to value HospCode in Table2
oleComm.CommandText="SELEC
dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()
da.selectCommand=Olecomm
da.Fill(ds,"table2")
'---Hosp1 is the dropdownlist control
Hosp1.datasource=ds.Tables
oleConn.Close()
hosp1.databind()
'-----------------END EXAMPLE 2-------------------------
Thank you in advance!
Kittrick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your responses!
Here is how I solved the problem for everyone to see. My problem was that I could find the index checked in table2
and the proper value corresponding to it in table1, but couldn't get the proper selectedindex to show in the dropdownlist and all of the other list options to show from table2. Either the selectedindex ONLY showed up from table1, or the whole list showed up in table2 with no selection highlighted. My solution involved WHERE I put my selectedindex command. I had it before the DataBind() call and now that I moved it afterthe call, it works fine. Here is my code that works:
<<<<<<<<<START CODE>>>>>>>>>>>
'-->Declaring the OLEDB Connection
Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString=" Provider=M icrosoft.J et.OLEDB.4 .0;data source=" & server.mappath("\test\prot otype.mdb" )
Dim oleComm AS NEW OleDb.oleDbCommand()
oleComm.Connection=oleConn
'--->SQL command that ONLY grabs the data from table2
oleComm.CommandText="SELEC T * FROM table2"
'--> Using the dataadapter, because user will eventually change list values on-the-fly
dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()
da.selectCommand=Olecomm
da.Fill(ds,"table2")
'--> Hosp1 is the dropdownlist
Hosp1.datasource=ds.Tables ("table2") .DefaultVi ew
oleConn.Close()
hosp1.databind()
'---->This was the problem code. I had it above the databind() call. Once moved below, it works!
Hosp1.Selectedindex = Hosp1.Items.IndexOf(Hosp1. Items.Find ByValue(Ho sp.text))
'--->Printing text for verification of table1(Hosp.text) and table2(selecteditem,select edindex) being equal
dim first as string="The hospital in spot on the dropdown is "
dim last as string =" and hospital "
response.write(first)
response.write(Hosp1.selec tedindex)
response.write(" ")
response.write(Hosp1.selec teditem)
response.write(last)
response.write(Hosp.text)
<<<<<<<<<<<<END CODE>>>>>>>>>>>>>>>
Here is how I solved the problem for everyone to see. My problem was that I could find the index checked in table2
and the proper value corresponding to it in table1, but couldn't get the proper selectedindex to show in the dropdownlist and all of the other list options to show from table2. Either the selectedindex ONLY showed up from table1, or the whole list showed up in table2 with no selection highlighted. My solution involved WHERE I put my selectedindex command. I had it before the DataBind() call and now that I moved it afterthe call, it works fine. Here is my code that works:
<<<<<<<<<START CODE>>>>>>>>>>>
'-->Declaring the OLEDB Connection
Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString="
Dim oleComm AS NEW OleDb.oleDbCommand()
oleComm.Connection=oleConn
'--->SQL command that ONLY grabs the data from table2
oleComm.CommandText="SELEC
'--> Using the dataadapter, because user will eventually change list values on-the-fly
dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()
da.selectCommand=Olecomm
da.Fill(ds,"table2")
'--> Hosp1 is the dropdownlist
Hosp1.datasource=ds.Tables
oleConn.Close()
hosp1.databind()
'---->This was the problem code. I had it above the databind() call. Once moved below, it works!
Hosp1.Selectedindex = Hosp1.Items.IndexOf(Hosp1.
'--->Printing text for verification of table1(Hosp.text) and table2(selecteditem,select
dim first as string="The hospital in spot on the dropdown is "
dim last as string =" and hospital "
response.write(first)
response.write(Hosp1.selec
response.write(" ")
response.write(Hosp1.selec
response.write(last)
response.write(Hosp.text)
<<<<<<<<<<<<END CODE>>>>>>>>>>>>>>>
ASKER