Click here to Skip to main content
15,915,761 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am having problems in retrieving data from database to ListBox.
Please give me an idea through which i can do it.

Thanks.
Posted
Updated 31-Mar-12 2:10am
v2
Comments
André Kraak 31-Mar-12 8:10am    
Edited question:
Spelling/Grammar
Replaced txt speak
Removed unnecessary tags

Hi,
Try this will guid you
C#
DataClassesDataContext Db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //retrieve data from Db through linq Ado.net ....etc
            var result = from x in Db.Genders
                         select new { x.gender1, x.test };
            //passing the datasource for control
            ListBox1.DataSource = result;
            // here the the value will dispaly
            ListBox1.DataTextField = "gender1";
            // value field
            ListBox1.DataValueField = "test";
            //bind control
            ListBox1.DataBind();
            //passing datasource
                GridView1.DataSource = result;
                GridView1.DataBind();
        }
    }



ASP.NET
<div>
      <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
      <br />
      <br />
      <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>
  </div>

Best Regards
M.mitwalli
 
Share this answer
 
Hi..

If you want to bind single record then it is fine the list box directly in following way

listbox.datasource=bll.getdatafromserver();
listbox.databind();

But if you bind data table to list bix it will become "," seprated in each row like

deepak,tushar,mohit

otherwise you can use..
http://asp-net-example.blogspot.in/2009/03/how-to-data-bind-listbox.html[^]

http://stackoverflow.com/questions/1143639/binding-multiple-fields-to-listbox-in-asp-net[^]

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox.aspx[^]

Hope it will help you

Thanks,
 
Share this answer
 
What have you tried so far? What errors are you getting?

If you need tutorials on how to binding list box
Check this[^]
 
Share this answer
 
hi there,

use select query dear.

for more details edit your question.
 
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