array in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flipperdog
    New Member
    • Mar 2008
    • 4

    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.Select ed == true)
    {
    TextBox2.Text += "\n" + LstItem.Text;

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


    }
    else
    {
    Alert("No Courses Selected");
    }
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    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 GetSelectedIndi ces() instead of iterating every item in your list increases performance.
    Code:
    int32[] Selected() = DropDownList1.GetSelectedIndices();
    foreach(int i in Selected){
    List.Add(DropDownList1.Items(i));
    }

    Comment

    • flipperdog
      New Member
      • Mar 2008
      • 4

      #3
      thanks for the advise is there any way to get the values from this and pass them into a database

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        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.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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.

          Comment

          • flipperdog
            New Member
            • Mar 2008
            • 4

            #6
            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)

            Comment

            • flipperdog
              New Member
              • Mar 2008
              • 4

              #7
              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.Inser tGroup(name.Tex t,temp1, temp2,temp3);

              Comment

              • balabaster
                Recognized Expert Contributor
                • Mar 2007
                • 798

                #8
                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.Inser tGroup(name.Tex t,temp1, temp2,temp3);
                What if you have more than 3 items in your listbox?

                Comment

                Working...