Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

array in c#

Question posted by: flipperdog (Newbie) on March 26th, 2008 09:44 PM
I have a drop list where you can select multiple options (at least 3) and i want to pass the selected values into an array these selected values are also shown in a text box i also have a text box called name i need to know how to take the 3 values from the array and the name entered in the text box and enter them into a database table where i have 3 columns name, e1,e2,and e3 here is what i have so far
or is there a better way to get the selected values from the drop down list and put them into the SQL database

foreach (ListItem LstItem in ListBox1.Items)
{
if (LstItem.Selected == true)
{
TextBox2.Text += "\n" + LstItem.Text;

ArrayList list = new ArrayList();
foreach (ListItem a in ListBox1.Items)
list.Add(LstItem.Selected);


}
else
{
Alert("No Courses Selected");
}
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
balabaster's Avatar
balabaster
Expert
399 Posts
March 27th, 2008
01:40 AM
#2

Re: array in c#
Although this doesn't answer your question (and I don't know the extent of your list box) but depending on how many items, you might find that referencing the method GetSelectedIndices() instead of iterating every item in your list increases performance.
Code: ( text )
  1. int32[] Selected() = DropDownList1.GetSelectedIndices();
  2. foreach(int i in Selected){
  3. List.Add(DropDownList1.Items(i));
  4. }

Reply
flipperdog's Avatar
flipperdog
Newbie
4 Posts
March 27th, 2008
02:31 PM
#3

Re: array in c#
thanks for the advise is there any way to get the values from this and pass them into a database

Reply
balabaster's Avatar
balabaster
Expert
399 Posts
March 27th, 2008
03:08 PM
#4

Re: array in c#
Quote:
Originally Posted by flipperdog
thanks for the advise is there any way to get the values from this and pass them into a database


The easiest way is to hook up a DataTable which you can add rows to by using arrays. For each row in the resulting array you've created take the text box information and the items in the array, create a DataRow putting the data into the data row, append it to the DataTable and then update the SQL database using a DataAdapter. For each new row in the DataTable insert a new row in your database.

Reply
Plater's Avatar
Plater
Moderator
5,267 Posts
March 27th, 2008
03:18 PM
#5

Re: array in c#
Also, you appear to be declaring your arraylist over and over again inside the foreach loop.
If you continue with checking each item for selected status, you will want to move the declaration of the Arraylist out and above the loop.

Reply
flipperdog's Avatar
flipperdog
Newbie
4 Posts
March 27th, 2008
04:12 PM
#6

Re: array in c#
is there a way to just do away with the array and take the values from my text box and insert them into my database for example in my multi line text box i have the three values and need to insert them along with the value from a text box called name into my table with 4 columns (name,e1,e2,e3)

Reply
flipperdog's Avatar
flipperdog
Newbie
4 Posts
March 27th, 2008
08:17 PM
#7

Re: array in c#
thanks for the advise i got it figured out instead of using an array
i put my values in a list box and assigned each value a variable using count and switch

int count = 0;
int i = 1;

count = ListBox2.Items.Count;
string temp1 = "";
string temp2 = "";
string temp3 = "";

foreach (ListItem LstItem in ListBox2.Items)
{
switch (i)
{
case 1:
temp1 = LstItem.Text; break;
case 2:
temp2 = LstItem.Text; break;
case 3:
temp3 = LstItem.Text; break;
}
i++;

then was able to insert each variable into my database

inserted = insertion.InsertGroup(name.Text,temp1, temp2,temp3);

Reply
balabaster's Avatar
balabaster
Expert
399 Posts
March 27th, 2008
08:44 PM
#8

Re: array in c#
Quote:
Originally Posted by flipperdog
thanks for the advise i got it figured out instead of using an array
i put my values in a list box and assigned each value a variable using count and switch

int count = 0;
int i = 1;

count = ListBox2.Items.Count;
string temp1 = "";
string temp2 = "";
string temp3 = "";

foreach (ListItem LstItem in ListBox2.Items)
{
switch (i)
{
case 1:
temp1 = LstItem.Text; break;
case 2:
temp2 = LstItem.Text; break;
case 3:
temp3 = LstItem.Text; break;
}
i++;

then was able to insert each variable into my database

inserted = insertion.InsertGroup(name.Text,temp1, temp2,temp3);


What if you have more than 3 items in your listbox?

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,814 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top .NET Forum Contributors