Link to home
Start Free TrialLog in
Avatar of neha_john
neha_john

asked on

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

can u pls tell me how to show data in the listbox.
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

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();
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
Avatar of neha_john
neha_john

ASKER

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 .
Do you mean that you want to write a setter property?

object RoleDataSource
{
  set
  {
    this.listbox1.SqlDataSource1=value;
    listbox1.databind();
  }
}
yes philip.....i wanted to use set property......how do i do that..pls help me..
Have you tried my snippet above?
Comment ID:22973046
yes i did..error is showing on SqlDataSource1.
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?
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();
  }
}



>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
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.
ASKER CERTIFIED SOLUTION
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok let me try..thanks Philip...