Friday, March 23, 2012

Is it possible to dispaly all columns for a record in a dropdownlist

Say a record has four columns in { Emp#, firstName, secondName, thirdName, spouseName }.

Now if you configure a sql datasource for a dropdownlist to return all columns, only Emp# will be displayed in the dropdownlist, the other columns wont be listed.

Is there a way to list the values of all columns in the dropdownlist using the sql datasource or in ADO.net code.

Thanks.

You have 5 columns there... but I think I see what you mean.

Anyway, the easiest way to do this is on your SQL.

"SELECT Emp#, firstname + ' ' + secondname + ' ' + thirdname + ' ' + spouseName AS TheName FROM table". Then set Emp# as the DataValueField value and TheName as the DataTextField value

|||

Mikesdotnetting:

"SELECT Emp#, firstname + ' ' + secondname + ' ' + thirdname + ' ' + spouseName AS TheName FROM table". Then set Emp# as the DataValueField value and TheName as the DataTextField value

But this would be listed as one dropdownlist value. I want each column to be listed in the dropdownlist as a new value. Since they are five columns, then i want them as five dropdownlist values.|||

Nope - sorry. Don't follow you. Do you mean you want them like this:

<item>firstname1</item>
<item>secondanme1</item>
<item>thirdname1</item>
<item>spousename1</item>
<item>firstname2</item>
<item>secondname2</item>

etc or do you mean like this:http://www.codeproject.com/aspnet/MultiColDdList.asp ?

|||

Mikesdotnetting:

Do you mean you want them like this:

<item>firstname1</item>
<item>secondanme1</item>
<item>thirdname1</item>
<item>spousename1</item>
<item>firstname2</item>
<item>secondname2</item>

Exactly.

But we would have to leave out firstname2 and secondname2. It would be like so

EMPLOYEE_1

<item>firstname1</item>
<item>secondanme1</item>
<item>thirdname1</item>
<item>spousename1</item>

EMPLOYEE_2

<item>firstname2</item>
<item>secondanme2</item>
<item>thirdname2</item>
<item>spousename2</item>

EMPLOYEE_3

<item>firstname3</item>
<item>secondanme3</item>
<item>thirdname3</item>
<item>spousename3</item>

Now notice that only one employee firstbname, secondname, thirdname and spousename would be listed in droopdownlist at a time and that is why i said we will leave out we would have to leave out firstname2 and secondname2 in your quoted example code above. But infact you have got the exact logic i want.

|||

Ok. You can get the record for each employee in your code-behind and loop through the fields in the DataReader, appending items to your dropdownlist as you go.

[Not complete or tested - just intended to convey the general idea]

while(rdr.Read())
{
for(int i = 0;i < rdr.FieldCount;i++)
{
MyDropdownlist.Items.Add(rdr[i].ToString());
}
}

|||

SELECT FirstName AS Value FROM MyTable WHEREEmpID=@.EmpID

UNION

SELECT SecondName AS Value FROM MyTable WHEREEmpID=@.EmpID

UNION

SELECT SpouseName AS Value FROM MyTable WHEREEmpID=@.EmpID

|||

Almost all the replies so far work in regard to displaying the values in the dropdownlist but not in retaining the selectedindex on page postback.

Now i have discovered why the selectedindex is always being read as 0 but i do not know how to resolve the problem, but atleast i now know why.

This behavour is being caused by the presence of a multivew and its child views. I say so because if i move my databound controls outside of the multiview views, every thing runs great, when i put them back in the view, selected index is always 0. I have tested and confirmed that this is the cause. I however have no idea how to fix this even after googling for some days.

As more proof, if i go to my page's markup and look at multiview tag, it is underlined in red meaning some error and in the tooltip, the error says

"Validation(ASP.NET): Text can not be allowed between opening and closing tags of element multiview" .

Using a multiview and its child views made my application look smart and easy to navigate, but now here is the pain at last.

sql

No comments:

Post a Comment