Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now