Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have 2 listboxes on 1 form , i want each listbox to display data but from the same sql table/database, my code >
C#
private void addproduct_Load(object sender, EventArgs e)
        {
            DataSet st = new DataSet();
            string strConnectionString = "Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=tblproducts;Integrated Security=True";

            SqlConnection objconnection = new SqlConnection(strConnectionString);
            using (SqlCommand cmd = new SqlCommand("SELECT  [name,quantity,code,price] FROM [addproducts]",
            objconnection))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(st);
                }
            }

            var empList = st.Tables[0].AsEnumerable().Select(dataRow =>

            dataRow.Field<string>("name")).ToList();
            listBox1.DataSource = empList;

            listBox1.SelectedIndex = 0;
            



        }

so i want to show the quantity in listbox 2 , so ive tried copying the whole code and just change bits here and there, but i get an error on var emplist, tried just adding >

dataRow.Field<string>("name")).ToList();
listBox1.DataSource = empList;

listBox1.SelectedIndex = 0;

and just change the values but no luck, please point out where or what im doing wrong, thanks in advance
Posted
Updated 30-Aug-15 23:17pm
v2

1 solution

C#
var empList = set.Tables[0].AsEnumerable().Select(dataRow => dataRow.Field<string>("name")).ToList();
ListBox1.DataSource = empList;
ListBox1.SelectedIndex = 0;
ListBox1.DataBind();

empList = set.Tables[0].AsEnumerable().Select(dataRow => dataRow.Field<string>("quantity")).ToList();
ListBox2.DataSource = empList;
ListBox2.SelectedIndex = 0;
ListBox2.DataBind();
</string></string>


You just have to use DataBind() method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900