• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

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

can u pls tell me how to show data in the listbox.
0
neha_john
Asked:
neha_john
  • 6
  • 6
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
neha_johnAuthor Commented:
ok let me try..thanks Philip...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now