Solved

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

Posted on 2004-04-23
3
823 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

790 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