Solved

Retrieve field from SQLDataSource selected in bound DropDownList

Posted on 2013-01-23
4
816 Views
Last Modified: 2013-01-28
I have an ASP.Net web application and have a drop down list which is bound to a SQLDataSource (SQL: SELECT ID, FileName, FileLocation FROM FILES).   FileName is the display member in the list

When the user selects an item in the list I want to retrieve a the FileLocation and display it in a text box.  How can I get this value from the selected item in the DataSource?  I'm hoping I don't have to do another SQL query.

The user can change this displayed FileLocation and update it in the DB by clicking a Save button.  Can I update the SQLDataSource directly?
0
Comment
Question by:canuckconsulting
4 Comments
 
LVL 6

Accepted Solution

by:
esolve earned 350 total points
ID: 38810065
The easiest option is to set the SelectedValue to the FileLocation. On the selectedindexchange event of the dropdown you fill the textbox with the dropdown.selectedvalue

If the selectedvalue is bound to the ID, which you might need for other purposes you should try a different approach. Something like this might work:

You can use the selected event of the object datasource. This event fires after the datasource fetched data from the database.

In the case of the dropdown you probably dont have that much data so you can fill eg a temp dataset in the selected event which you can save in a session variable. This dataset can then later be retrieved from code behind on the dropdown selectedindexchanged event.

I think you need to create a seperate update code.
0
 
LVL 40
ID: 38810340
The SelectedItem property of the DropDownList gives you access to the underlying data.

Simply cast DropDownList.SelectedItem to the type of the underlying data and you will have access to all its properties and methods. If the control is bound to a DataTable for instance, I could retrieve value of the City field in the selected row with the following:

(string)((DataRow) DropDownList.SelectedItem)["City"];
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 150 total points
ID: 38813575
SqlConnection con = new SqlConnection(str);
        string com = "SELECT ID, FileName, FileLocation FROM FILES";
        SqlDataAdapter adpt = new SqlDataAdapter(com, con);
        DataTable dt = new DataTable();
        adpt.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();
        DropDownList1.DataTextField = "FileName";
        DropDownList1.DataValueField = "FileLocation ";
        DropDownList1.DataBind();

Set in  aspx : OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"

on selected index changed property ...
 Read the "SelectedText" Value field
and set that Value  to the Label control
0
 

Author Closing Comment

by:canuckconsulting
ID: 38828798
Thank you.

I was hoping for something like JamesBurger but this doesn't work in asp.net
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

14 Experts available now in Live!

Get 1:1 Help Now