简体   繁体   中英

Insert Multiple Rows from DropDown to SQL

I have 6 DropDownList that have the following values;

ABC,DEF,GHI,JKL,MNO,PQR

I want to insert the values from ddl1, ddl2, ddl3, ddl4, ddl5 and ddl6 into my SQL table by taking the value from each dropdownlist.

I've currently got the following but need to adapt it to insert the values from each individual dropdownlist rather than the same value:

SqlConnection sqlCon = new SqlConnection("Data Source=***; User ID=***; MultipleActiveResultSets=True; Password=***; Initial Catalog=PMRDA;");

try
{
    sqlCon.Open();

    foreach (string str in AllOptions)
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO lboard(eventType, date, college) values (@eventType, GETDATE(), @college)", sqlCon);

        SqlParameter eventType = cmd.Parameters.Add("@eventType", SqlDbType.VarChar);
        SqlParameter college = cmd.Parameters.Add("@college", SqlDbType.Char);

        eventType.Value = ddlEventType.SelectedItem.Value;
        college.Value = ddl5.SelectedItem.Value;

        cmd.ExecuteNonQuery();
    }

    LabelStatus.Text = "Submitted Succesfully";
}
catch(Exception ex)
{
    LabelStatus.Text = ex.Message;
}
finally
{
    sqlCon.Close();
}

in your foreach loop you have an allOptions string list (i'll assume this is the list with all the selectedValues from the ddl's)

change your

college.Value = ddl5.SelectedItem.Value;

To

college.Value = str;

else it would be quite astonishing you have a foreach loop with an object you never use ...

EDIT after comment;

Create a class that holds 2 string properties StringSome = your string to have, stringDrop = the dropdown selectedvalue

public class MyClass
{
 public string StringSome{get;set;}
 public stirng StringDrop{get;set;}
 public MyClass()
 {//default constructor}
 public MyClass(string a, string b)
 {
  StringSome = a; 
  StringDrop = b;}
 }

change

Alloptions = new List<string>();

to

Alloptions = new List<MyClass>();

Fill your class according to the data

i assume you want string1 to ddl1 and string2 to ddl2 etc...

example

AllOptions.Add(new MyClass(yourcontrol1.Text, ddl1.SelectedValue.Value));

keep the foreach loop on AllOptions

change

college.Value = str;

to

college.value = str.StringDrop;

use the str.StringSome for your other parameter

Iterate through your dropdown list, if list1 is that list:

  foreach (string str in list1)
{    
....
college.Value = str;
....
}

However by doing this it has no meaning having the values in dropdown listbox (thus selection looses its point of use) and you could simply store them in a simple List <string>

Just add a for statement like this for each of you comboboxes.

string text_to_add;
for (int i=0; i< comboBox.Items.Count; i++)
{
    text_to_add=comboBox.Items[i].ToString();
    //Perform the db insert using the text_to_add string
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM