dropdownlist databind problems

S

Srinivas

Hi,

My function returns a datareader object containing only one column of
datatype money. In asp.net I want to bind this datareader to a dropdownlist.
Can anyone suggest me how to do this with some sample code if possible.

Thanks

Srinivas
 
G

Gerald Klein

I found it best to load them in functions such as below.

public static void Loadlist(DropDownList thisDropDownList , SqlDataReader
dr)
{
thisDropDownList.Items.Clear();
ListItem li;
while(dr.Read())
{
li = new ListItem(dr[1].ToString().Trim(),dr[0].ToString().Trim());
thisDropDownList.Items.Add(li);
}
dr.Close();
}
 
M

Martha[MSFT]

You can assign your reader to the DropDownList control's "DataSource"
property and call the DataBind() method. You must also set the
DropDownList's "DataTextField" property to the column that you want to bind
to the control.
Here are some code pieces:

reader = command.ExecuteReader();
ddList.DataSource = reader;
ddList.DataTextField = "money";
ddList.DataBind();

<asp:DropDownList ID="ddList" Runat="Server">

Hope this helps,
Martha
 
S

Srinivas

Thanks

I have tried the code. Its working fine. But the amounts are displayed as
100.000 as the datatype of the column from which the data is retrieved is
money. Is there any way to display the value 100.000 as 100

Srinivas
 
Joined
Jan 7, 2009
Messages
1
Reaction score
0
try this simplest way...

string connectionStringv = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Niranjan\My Documents\Visual Studio 2008\db2.mdb";
OleDbConnection connv = new OleDbConnection(connectionStringv);
connv.Open();

string sqlv = "SELECT name from table1";


OleDbCommand cmdv = new OleDbCommand(sqlv, connv);



OleDbDataReader readerv = cmdv.ExecuteReader();

while (readerv.Read())
{
DropDownList1.Items.Add(readerv.GetString(0));
}

readerv.Close();
connv.Close();
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top