473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

array in c#

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");
}
Mar 26 '08 #1
7 1123
balabaster
797 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. int32[] Selected() = DropDownList1.GetSelectedIndices();
  2. foreach(int i in Selected){
  3. List.Add(DropDownList1.Items(i));
  4. }
Mar 27 '08 #2
thanks for the advise is there any way to get the values from this and pass them into a database
Mar 27 '08 #3
balabaster
797 Expert 512MB
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.
Mar 27 '08 #4
Plater
7,872 Expert 4TB
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.
Mar 27 '08 #5
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)
Mar 27 '08 #6
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);
Mar 27 '08 #7
balabaster
797 Expert 512MB
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?
Mar 27 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.