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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sathya NarayanPosted Jan 17, 2014, 1:19 AM
Sathya NarayanPosted Jan 17, 2014, 1:16 AM
VulpesPosted Jan 2, 2014, 5:51 AM
samantaPosted Jan 1, 2014, 10:57 PM
objCommand.CommandText = "SELECT [cat_ID] " +
" ,[cat_Category]" +
" ,[cat_OrderBy]" +
" FROM [itdatahouse].[dbo].[tblCategory]" +
" ORDER BY [cat_OrderBy]";
this is my query
VulpesPosted Dec 31, 2013, 8:33 AM
samantaPosted Dec 31, 2013, 7:43 AM
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);
}
VulpesPosted Dec 31, 2013, 7:39 AM
samantaPosted Dec 31, 2013, 2:33 AM
{
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.
Sanjeeb LenkaPosted Dec 31, 2013, 2:28 AM
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
samantaPosted Dec 31, 2013, 2:17 AM
once submit form listbox2 items ID stored in database.
Sanjeeb LenkaPosted Dec 31, 2013, 2:14 AM
samantaPosted Dec 31, 2013, 2:10 AM
but when i add selected listbox1 items to listbox2, listbox2 items must be sorted based on order
Sanjeeb LenkaPosted Dec 31, 2013, 2:07 AM
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.
samantaPosted Dec 31, 2013, 2:05 AM
samantaPosted Dec 31, 2013, 2:01 AM
{
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();
Sanjeeb LenkaPosted Dec 31, 2013, 1:41 AM
or try like this query
select * from employe order by orderid desc
sha ikPosted Dec 31, 2013, 1:33 AM
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.
samantaPosted Dec 31, 2013, 12:01 AM
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.
Sanjeeb LenkaPosted Dec 30, 2013, 11:53 PM
samantaPosted Dec 30, 2013, 11:44 PM
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 LenkaPosted Dec 30, 2013, 11:40 PM
samantaPosted Dec 30, 2013, 11:28 PM
Satyapriya NayakPosted Dec 30, 2013, 11:20 PM
<%@ 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