?
Solved

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

Posted on 2004-04-23
3
Medium Priority
?
842 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 28

Accepted Solution

by:
mmarinov earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 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