Solved

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

Posted on 2004-04-23
3
822 Views
Last Modified: 2012-06-22
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()
0
Comment
Question by:Kittrick
3 Comments
 
LVL 2

Expert Comment

by:mufaddal
ID: 10906269
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")
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10907075
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 10912237
you have to modify your code!
reason :
before binding the dropdownlist control the selected index is the first one
to get changes your code ( FindByValue ) you have to bind the DropDownList so you will have values the the .net will search through

so
---> 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.databind() //////////////////////////////////// This should stands here
//////////////////////////////////////////////////////// Not at the end


'------> 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()

You also have to use DataTextField and DataValueFiled to set up which column from the datatable is marked for the value of the DropDownList and for the displayed value

B..M
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question