Solved

Retrieve field from SQLDataSource selected in bound DropDownList

Posted on 2013-01-23
4
826 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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