how to show records in the listbox from the database??

can u pls tell me how to show data in the listbox.
neha_johnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

philipjonathanCommented:
SqlConnection connection = new SqlConnection("...connection string here...");
SqlCommand cmd = new SqlCommand("SELECT item_id, item_name FROM table", connection);
SqlDataReader reader = cmd.ExecuteReader();

listbox1.DataTextField = "item_name";
listbox1.DataValueField = "item_id";
listbox1.DataSource = reader;
listbox1.DataBind();

reader.Close();
connection.Close();
0
lotusnotesnewbieCommented:
Or without using codebehind, we can achieve it like

    <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Cat"
        DataValueField="ID"></asp:ListBox><asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:myprojdb %>" SelectCommand="SELECT ID,Cat FROM [test]">
        </asp:SqlDataSource>

where myprojdb is the name of the connection string defined in web.config, "Change the select command accordingly with the required column names"

happy programming
0
neha_johnAuthor Commented:
ok actually we are using model view presenter pattern in c#.net web application.In the ascx file i wrote like
<asp:ListBox Id="ListBox1" runat="Server" TextField="RoleName"
        ValueField="RoleId"></asp:ListBox>
and in ASCX.cs file i wrote like
object RoleDataSource  // I am getting error over here.
{
this.listbox1.SqlDataSource1=value;
listbox1.databind();
}
here to bind records ,i will get records from the presenter by calling a sevice called getAllGroups().
can u pls help me to bind records which we got from the service to a listbox .
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

philipjonathanCommented:
Do you mean that you want to write a setter property?

object RoleDataSource
{
  set
  {
    this.listbox1.SqlDataSource1=value;
    listbox1.databind();
  }
}
0
neha_johnAuthor Commented:
yes philip.....i wanted to use set property......how do i do that..pls help me..
0
philipjonathanCommented:
Have you tried my snippet above?
Comment ID:22973046
0
neha_johnAuthor Commented:
yes i did..error is showing on SqlDataSource1.
0
philipjonathanCommented:
Did you add that within the class scope? i.e.:

public class MyPage ....
{
  object RoleDataSource
  {
    set
    {
      this.listbox1.SqlDataSource1=value;
      listbox1.databind();
    }
  }
}


Do you mind attaching your aspx.cs here?
0
neha_johnAuthor Commented:
hi Philip,

i have a method in controller(Model View Presenter) like
public string[] GetAllUserGroups()
        {
            return Roles.GetAllRoles();
        }
in the interface
string[] DataSource { set;}
and in the ascx.cs file to show one row of records in a grid view
public string[] DataSouce ()  //to bind in the gridview
{ set
  {
    this.aspxgrid.DataSource=value;
    aspxgrid.databind();
  }
}



0
philipjonathanCommented:
>public string[] DataSouce ()  //to bind in the gridview

To declare a setter property, the property name must not end with ().
Change to:
public string[] DataSource
0
neha_johnAuthor Commented:
Hi Philip, Thanks for ur consideration..here to add rolenames to a listbox from the database.i wrote in ascx file:
<dxe:ASPxListBox ID="ASPxListBox1" runat="server" Height="262px" Width="252px" TextField="RoleName" ValueField="RoleId" />
  //<%--DataSourceID="XmlDataSource1"--%>  i dont know this part. iam getting value from controller (MVP) like this  as public string[] GetAllUserGroups()
        {
            return Roles.GetAllRoles();
        }
In presenter:
base.View.DataSource = this._controller.GetAllUserGroups();
and in ascx.cs file
public string[] DataSource
    {
        set
        {
             this.ASPxListBox1.DataSourceID = value;
            this.ASPxListBox1.DataBind();
}}  iam getting error like Cannot implicitly convert type 'string[]' to 'string'..
do i need to create a xml document and assign it to the DataSourceID ..if so,pls let me know wht i need to do for that...pls help me.. or is there any trouble with array of string to assign listbox.
0
philipjonathanCommented:
Try changing DataSourceID to DataSource:

public string[] DataSource
{
  set
  {
    this.ASPxListBox1.DataSource = value;
    this.ASPxListBox1.DataBind();
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
neha_johnAuthor Commented:
ok let me try..thanks Philip...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.