In database table i have 3 columns like Text | Values | Order
i want to add these columns data to listbox. how to do it.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 30, 2013, 6:20 PM EST
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
Default.aspx.vb code
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con.Open()
str = "select * from employee"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "employee")
ListBox1.DataValueField = "empid"
ListBox1.DataTextField = "empname"
ListBox1.DataSource = ds
ListBox1.DataMember = "employee"
ListBox1.DataBind()
con.Close()
End Sub
End Class
samantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 6:28 PM EST
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Dec 30, 2013, 6:40 PM EST
samantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 6:44 PM EST
so i want to add database data to listbox1 which is sorted based on order and value must be preserved. how can i do this?
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Dec 30, 2013, 6:53 PM EST
partial class _Default : System.Web.UI.Page { string strConnString = "connection string"; SqlConnection con = new SqlConnection(strConnString); string str; SqlCommand com; SqlDataAdapter sqlda; DataSet ds; protected void Page_Load(object sender, System.EventArgs e) { con.Open(); str = "select * from employee"; com = new SqlCommand(str, con); sqlda = new SqlDataAdapter(com); ds = new DataSet(); sqlda.Fill(ds, "employee"); ListBox1.DataValueField = "empid"; ListBox1.DataTextField = "empname"; ListBox1.DataSource = ds; ListBox1.DataMember = "employee"; ListBox1.DataBind(); con.Close(); } } to copy item from one list box to other refer to this link http://www.aspdotnet-suresh.com/2010/12/how-to-move-listitems-from-one-listbox.htmlsamantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 7:01 PM EST
when i add selected listbox1 items to listbox2, listbox2 items must be sorted based on order and when i submit my form listbox2 items value must be stored in database.
once i removed selected listbox2 items, all selected items must be added back to listbox1 with sorted by order.
sha ik12 reputationPosted Dec 30, 2013, 8:33 PM EST
You can send qurey/in Stored procedure with order by clause,Then you can get your result set
in sorting order.
Try using list to bind, before adding and deleting items to list box do order in linq query.
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Dec 30, 2013, 8:41 PM EST
or try like this query
select * from employe order by orderid desc
samantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 9:01 PM EST
{
Initalize();
if (!IsPostBack)
{
LoadCatagory();
}
void LoadCatagory()
{
SqlCommand objCommand = new SqlCommand();
objCommand.CommandType = CommandType.Text;
objCommand.Connection = _databaseConnection;
objCommand.CommandText = "SELECT [cat_ID] " +
" ,[cat_Category]" +
" FROM [itdatahouse].[dbo].[tblCategory]" +
" ORDER BY [cat_Category]";
SqlDataReader objRs = objCommand.ExecuteReader();
while (objRs.Read())
{
ListItem objList = new ListItem();
objList.Text = objRs["cat_Category"].ToString();
objList.Value = objRs["cat_ID"].ToString();
lstcom_Category.Items.Add(objList);
}
objRs.Close();
objCommand.Dispose();
samantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 9:05 PM EST
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Dec 30, 2013, 9:07 PM EST
if you want oder by cat_id descending use this:
objCommand.CommandText = "SELECT [cat_ID],[cat_Category] FROM [itdatahouse].[dbo].[tblCategory] ORDER BY [cat_ID] desc";
by default its ascending.
samantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 9:10 PM EST
but when i add selected listbox1 items to listbox2, listbox2 items must be sorted based on order
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Dec 30, 2013, 9:14 PM EST
samantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 9:17 PM EST
once submit form listbox2 items ID stored in database.
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Dec 30, 2013, 9:28 PM EST
DataTable table = new DataTable();
table.Columns.Add("Catid", typeof(int));
table.Columns.Add("Cat", typeof(string));
for add a new row
DataRow dr = dt.NewRow();
dr["Catid"] = catidfromlistbox1;
dr["Cat"] = catgoryfromlistbox1;
dt.Rows.Add(dr);
to sort it
dt.DefaultView.Sort = "Catid desc";
then bind it to listbox2
samantaRank #3,157 · 454 reputationPosted Dec 30, 2013, 9:33 PM EST
{
ListItem objLIst = new ListItem();
int count = listbox1.Items.Count;
string[,] lstSource = new string[2, count];
for (int i = 0; i < count; i++)
{
lstSource[0, i] = listbox1.Items[i].Value;
lstSource[1, i] = listbox1.Items[i].Text;
}
int count2 = lstSource.GetLength(1);
ListItem li = null;
for (int i = 0; i < count2; i++)
{
if (listbox1.Items[i].Selected)
{
li = new ListItem(lstSource[1, i], lstSource[0, i]);
listbox2.Items.Add(li);
}
}
}
this is how i added listbox1 to listbox2. but i want to sort this listbox2 items based on order not id.
VulpesRank #55 · 29.2K reputationPosted Dec 31, 2013, 2:39 AM EST
samantaRank #3,157 · 454 reputationPosted Dec 31, 2013, 2:43 AM EST
because i already added like this
while (objRs.Read())
{
ListItem objList = new ListItem();
objList.Text = objRs["cat_Category"].ToString();
objList.Value = objRs["cat_ID"].ToString();
listbox1.Items.Add(objList);
}
VulpesRank #55 · 29.2K reputationPosted Dec 31, 2013, 3:33 AM EST
samantaRank #3,157 · 454 reputationPosted Jan 1, 2014, 5:57 PM EST
objCommand.CommandText = "SELECT [cat_ID] " +
" ,[cat_Category]" +
" ,[cat_OrderBy]" +
" FROM [itdatahouse].[dbo].[tblCategory]" +
" ORDER BY [cat_OrderBy]";
this is my query
VulpesRank #55 · 29.2K reputationPosted Jan 2, 2014, 12:51 AM EST
Sathya Narayan148 reputationPosted Jan 16, 2014, 8:16 PM EST
Sathya Narayan148 reputationPosted Jan 16, 2014, 8:19 PM EST