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.
Or without using codebehind, we can achieve it like
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSourc e1" DataTextField="Cat"
DataValueField="ID"></asp: ListBox><a sp:SqlData Source 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
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSourc
DataValueField="ID"></asp:
ConnectionString="<%$ ConnectionStrings:myprojdb
</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
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.SqlDataSourc e1=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 .
<asp:ListBox Id="ListBox1" runat="Server" TextField="RoleName"
ValueField="RoleId"></asp:
and in ASCX.cs file i wrote like
object RoleDataSource // I am getting error over here.
{
this.listbox1.SqlDataSourc
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.SqlDataSourc e1=value;
listbox1.databind();
}
}
object RoleDataSource
{
set
{
this.listbox1.SqlDataSourc
listbox1.databind();
}
}
ASKER
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
Comment ID:22973046
ASKER
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.SqlDataSourc e1=value;
listbox1.databind();
}
}
}
Do you mind attaching your aspx.cs here?
public class MyPage ....
{
object RoleDataSource
{
set
{
this.listbox1.SqlDataSourc
listbox1.databind();
}
}
}
Do you mind attaching your aspx.cs here?
ASKER
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=v alue;
aspxgrid.databind();
}
}
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=v
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
To declare a setter property, the property name must not end with ().
Change to:
public string[] DataSource
ASKER
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="XmlDat aSource1"- -%> 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.GetAllUse rGroups();
and in ascx.cs file
public string[] DataSource
{
set
{
this.ASPxListBox1.DataSour ceID = 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.
<dxe:ASPxListBox ID="ASPxListBox1" runat="server" Height="262px" Width="252px" TextField="RoleName" ValueField="RoleId" />
//<%--DataSourceID="XmlDat
{
return Roles.GetAllRoles();
}
In presenter:
base.View.DataSource = this._controller.GetAllUse
and in ascx.cs file
public string[] DataSource
{
set
{
this.ASPxListBox1.DataSour
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok let me try..thanks Philip...
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();