Jump to content

HOW TO read from a database INTO A LISTBOX


LJnel

Recommended Posts

helo,i am using a MS access database and i just want to display a number of subjects from(READ) the database subjects table and populate the first listbox..i am going to use 2 listboxes from which you can add and remove subjects..similiar to to example in windows xp where you can customize your toolbar in windows explorer by going to VIEW --> TOOLBARS --> CUSTOMIZE, i have tried this in ASP.NET code but i cant seem to get it to work :):) first, i want to add subjects from the populated listbox and display the (subjects that u added by clicking on the "ADD" button) in the 2nd listbox and also remove them from the 2nd listbox(by clicking on the "REMOVE" button)..and write the contents from the 2nd listbox to the databaseplease help me, i know another way but that involves a different screen layout and it is not the same as the windows example..any code examples to read and write from the DB into the listboxthank you in advance Louis :)

Link to comment
Share on other sites

Try thisok so you have 2 multiline ddl and 2 buttons on your page...

private void Page_Load(object sender, EventArgs e){  if(!IsPostBack)  {	//setup your database objects here	//setup sql query	conn.Open();	dataReader = comm.ExecuteReader();	while(dataReader.Read())	{	  ddl1.Items.Add(new ListItem(dataReader["label"].ToString(),dataReader["value"].ToString()));	}	dataReader.Close();	conn.Close();  }  private void AddButton_Click(........)  {	 ddl2.Items.Add(ddl1.Items[ddl1.SelectedIndex]);	 ddl1.Items.Remove(ddl1.SelectedIndex);  }  private RemoveButton_Click(........)  {	 ddl1.Items.Add(ddl2.Items[ddl2.SelectedIndex]);	 ddl2.Items.Remove(ddl2.SelectedIndex);  }}

Link to comment
Share on other sites

helo,it is just 2 listboxes, the 1st listbox is already populated from the databse when the user arrives on the page.. the 2nd listbox is empty at first, when the user wants to add a subject from the 1st listbox it is added in the 2nd listbox, and the user can also remove it if its the wrong subject added, and when all the relevant subjects are in the 2nd listbox and the user submits, then i want to write it back to the database..i am not using any dropdownlist..do u understand now?, its not big at alli will be glad if u can help, i registered at your site as well.. :) Louis

Link to comment
Share on other sites

well the same concept applies to listbox and ddl. he 2 click functions I gave you pass values from one list to the other.I don't understand what you want to "write back" to the database.Is it possible for you to post a screen shot of your form and explain in a bit more detail what the requirements of this form are.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...