Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all :)

I have a SQLData source, connected to listbox as a wizard

I want to retrieve some data when I choose an item

suppose here is my table

Vedios:
- Video ID
- Video Name
- Video Path

I want to ( for example ) to retrieve the name and the path when I choose an Item inside my listbox and set it as a text in atextbox

how can I do that ?


thank you all :)
Posted
Comments
jim lahey 20-Sep-11 10:58am    
tried anything at all yet?

Hi,

Here 'm providing my code for showing data in textbox when selection changed in listbox

C#
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    MylocalDataClassesDataContext db = new MylocalDataClassesDataContext();
    var h = from g in db.VideoTabs
            where g.vid == int.Parse(ListBox1.SelectedValue)
            select g;
    foreach (VideoTab vt in h)
    {
        TextBox1.Text ="The Vid of selected Item is "+ vt.vid + " and Video Name is     " + vt.vname;
    }
}


Here I used Linq for retrieving data from database.

And onemore thing is you've to set autopostback to true for ListBox control by default it is false
All the Best.
 
Share this answer
 
Comments
rkthiyagarajan 20-Sep-11 11:35am    
Nice....
Anil Honey 206 20-Sep-11 23:26pm    
good
I would try this. The CreateData would be your SQLDataSource
set the video path as the datavalue and the video name as the datatextfield
Be sure that you have auto postback set to true on the dropdown and set the text box text when the drop down selection is changed.

protected void  Page_Load(object sender, System.EventArgs e)
{
	if (!IsPostBack) {
		LoadDropDown();
	}

}


private List<VideosAndPaths> CreateVideoData()
{
	List<VideosAndPaths> listOfVideoAndPaths = new List<VideosAndPaths>();
	VideosAndPaths newVAP = default(VideosAndPaths);

	for (i = 0; i <= 10; i++) {
		newVAP = new VideosAndPaths();
		newVAP.VideoID = i;
		newVAP.VideoName = "Video #" + i.ToString;
		newVAP.VideoPath = "C:\\MyVideos\\" + newVAP.VideoName + ".avi";
		listOfVideoAndPaths.Add(newVAP);

	}

	return listOfVideoAndPaths;
}


private void LoadDropDown()
{
	this.ddlVideoList.DataTextField = "VideoName";
	this.ddlVideoList.DataValueField = "VideoPath";
	this.ddlVideoList.DataSource = CreateVideoData();
	this.ddlVideoList.DataBind();

	ListItem li = new ListItem("select Video", "-1");
	li.Selected = true;

	this.ddlVideoList.Items.Insert(1, li);

}


protected ddlVideoList_SelectedIndexChanged(object sender, EventArgs e)
{
	if (this.ddlVideoList.SelectedValue != "-1") {
		this.txtVideoPath.Text = this.ddlVideoList.SelectedValue;
	} else {
		this.txtVideoPath.Text = "";
	}
}


public class VideosAndPaths
{
	private int _videoID;
	public int VideoID {
		get { return _videoID; }
		set { _videoID = value; }
	}

	private string _videoName;
	public string VideoName {
		get { return _videoName; }
		set { _videoName = value; }
	}

	private string _videoPath;
	public string VideoPath {
		get { return _videoPath; }
		set { _videoPath = value; }
	}



}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900