ListBox and sqldatasource

W

walanta

Hi,

I have a ListBox that bind to a sql source with managed code?

I know how to print ListBox1.SelectedItem and ListBox1.SelectedValue.
to print two fields from the following select statement.
But how can I print other fields in my select statement? I guess it
should be ListBox1.something...??

Suppose my select statement is like :

"select custid, name1 as firstname, name2 as lastname, address2 as
address from customer where custid=@custid"
 
E

Eliyahu Goldin

ListBox is able to bind only to 2 fields from the datasource: one for the
text and another for the value. It has no idea about any other fields. You
can use the selected item to find the row for the item in the datasource and
get other fields from the datasource itself.
 
W

walanta

I'm not sure I understand your second sentence.
Could you give me an quick example or point me to where I can get the
information or example? Thanks!!!
 
E

Eliyahu Goldin

Asp.Net renders server control ListBox as certain html element rendered by
the client browser. Datasource and databinding is needed to fill up the
element properties. These properties are the text and the value of the items
that the listbox contains. They are taken from the datasource fields. Other
fields that the datasource contains are not needed for the listbox
functionality. They are not available via the listbox.
 
Joined
Nov 9, 2006
Messages
4
Reaction score
0
Re: listbox and datasource

Hi,

When you bind a datasource for a .Net ListBox, its possible to access a all the fields of the row but you should have corresponding mapped c# class object declared.
for example if you are binding datasource from table employee{empId, empName, empAge, empSex}.

you should have a corresponding class( Class name can be anything but fieldname matters) like

class Employee{

int empID; //type should support implicitly casting,
string empName;
int empAge;
char empSex;
....
}

Employee myemp = new Employee();
myemp=(Employee) listbox1.SelectedItem;

now you can access all fields of the selected row from the datasource.


With luv,
rudresh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top