Solved

Retrieve field from SQLDataSource selected in bound DropDownList

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

12 Experts available now in Live!

Get 1:1 Help Now