0

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();
}
1
  • By dropdown list you mean a combobox right? Commented Feb 5, 2014 at 11:52

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Schuere - you are correct STR does contain the selected values, how can I also enter another string array of selected values at the same time?
0

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>

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.