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
Solved

Retrieve field from SQLDataSource selected in bound DropDownList

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

837 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