I have a a dropdown list that stores multiple items but i tried multiple things and i am not able to store them at all.
So the porpuse of this is: A user have to fill a form and in there they will have a dropdown list where they can select the orders number they want. I have tried to do a foreach but it didnt work. Some people told me to try with ajax but i sont really know how that works. Resume: I have to store the selected items and then insert them in my database table.
asp.net
<tr>
<td>
<asp:Label ID="Label7" runat="server" Text="Encomenda"></asp:Label>
</td>
<td>
<asp:DropDownList ID="multiple" style="width: 150px" runat="server" multiple></asp:DropDownList>
</td>
</tr>
</table>
</fieldset>
<asp:Button ID="ButtonCreate" runat="server" Text="Button" OnClick="ButtonCreate_Click" />
</div>
</div>
</div>
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Select2 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script>
$("#DropDownListPorto").select2({
/*placeholder: "Select a programming language",*/
allowClear: true
});
$("#DropDownListPais").select2({
/*placeholder: "Select a programming language",*/
allowClear: true
});
$("#multiple").select2({
/*placeholder: "Select a programming language",*/
allowClear: true
});
</script>
C#
protected void ButtonCreate_Click(object sender, EventArgs e)
{
if (txtBL.Enabled == true && txtBL.Text != "")
{
string email = "miguel.araujo@gatopreto.com";
string email1 = "andre.vicente@gatopreto.com";
string cc = "miguel.araujo@gatopreto.com";
string BLbooking = txtBL.Text;
if (string.IsNullOrWhiteSpace(txtBL.Text))
{
Erro.Visible = true;
}
else
{
if (txtBL.Text.Length > 5)
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Booking (BL, Data_booking) values (@BL, @date)", con);
cmd.Parameters.AddWithValue("@BL", BLbooking);
cmd.Parameters.AddWithValue("@date", DateTime.Now);
cmd.ExecuteNonQuery();
MailAddress from = new MailAddress("embarques@gatopreto.pt");
SmtpClient smtp = new SmtpClient("10.7.0.102");
smtp.EnableSsl = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
MailMessage msguser = new MailMessage();
msguser.Subject = "Novo Booking";
msguser.From = from;
msguser.CC.Add(cc);
msguser.Body = "Booking criado pela DSV. BL: " + BLbooking + " ";
msguser.IsBodyHtml = true;
msguser.To.Add(email);
msguser.To.Add(email1);
smtp.Send(msguser);
BookingSent.Visible = true;
txtBL.Text = "";
foreach (var item in multiple.SelectedValue)
{
SqlCommand cmd1 = new SqlCommand("INSERT INTO Linha_Encomendas (Produto) VALUES(@prod1);", con);
cmd1.Parameters.AddWithValue("@prod1", item.ToString());
cmd1.ExecuteNonQuery();
}
con.Close();
}
else
{
Erro.Visible = true;
}
}
}
else
{
Erro.Visible = true;
}
}