Click to See Complete Forum and Search --> : SqlDataSource problem


lhayes00
October 22nd, 2006, 01:24 PM
Hi all,

I am using Visual Web Developer 2005 Express Edition with the ASP.NET 2.0 framework. I have created an SQL database using SQL Express Edition 2005.

I have got a page which contains a drop down box. The control is bounded to the database via an SqlDataSource object. This works, and when running the drop down list shows all data records and when a data record is selected I can retrieve the ID value.

The database table has fields like:
[ID] [Name] [Description] [Date Modified]

Now I have access to the selected records, [ID] and [Name] via the DropDownList object. How can I access the other two fields for read-only purposes?

I am a complete newbie with ASP.NET, but am picking it up quite well.

Lea Hayes

Niranos
October 23rd, 2006, 06:14 AM
Hi

U can do this by creating a little bit different SQL statement:

SELECT ID, (Name + ‘ ‘ + Description + ‘ ‘ + Date Modified) as Fields FROM [YourTableName]

Then in DropDownList in field ‘Select a data field to Display’ write Fields, and in field
‘select a data field for value’ write ID

I hope this helps ^^

lhayes00
October 23rd, 2006, 06:26 AM
Hi,

Thanks for your advice, I will give it a try. You see what I wanted to do was have a DropDownList which shows the Name fields, and then beneath the DropDownList have a label control which shows the associated description. You have suggested that I put the three values within the data to display; but I am guessing that this will work as the data value as well, and then extract the strings afterwards.

Many thanks again, :)
Lea Hayes

Niranos
October 23rd, 2006, 07:35 AM
Aha, this Label change this problem a little XD

Depend on which fields You want to display in DropDownList and which in Label.Text, You need to prepare SELECT statement. Then change DropDownList property AutoPostBack = true, next You can use this code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Visible = true;
Label1.Text = DropDownList1.SelectedValue.ToString();
}


Greetings
Peter

lhayes00
October 23rd, 2006, 07:41 AM
That's brilliant, thanks for pointing out the AutoPostBack flag...I've been spending ages trying to find out why that control event wasn't firing.

Lea Hayes