?
Solved

Retrieve field from SQLDataSource selected in bound DropDownList

Posted on 2013-01-23
4
Medium Priority
?
838 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
[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
4 Comments
 
LVL 6

Accepted Solution

by:
esolve earned 1400 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 600 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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