Hey smile,
Please see below sample it's working.
HTML
MasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=ddlSubjects]").select2({
placeholder: "Select",
allowClear: false
});
$('[id*=ddlSubjects]').on('change', function () {
$('[id*=hfSelected]').val($(this).val());
});
});
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="row">
<div class="col-sm-12">
<label class="control-label" style="margin-top: 10px;">
Subject Name
</label>
<asp:DropDownList ID="ddlSubjects" runat="server" multiple="multiple" CssClass="form-control js-example-placeholder-single"
ToolTip="Select ">
</asp:DropDownList>
<asp:HiddenField ID="hfSelected" runat="server" />
<asp:Literal ID="Lit2" runat="server" />
<asp:Button ID="btnSave" Text="Save " runat="server" OnClick="btnSave_Click" />
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Default.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Country,CustomerId FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
ddlSubjects.DataSource = dt;
ddlSubjects.DataTextField = "Country";
ddlSubjects.DataValueField = "CustomerId";
ddlSubjects.DataBind();
}
}
}
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
string country = string.Empty;
string keyWords = hfSelected.Value;
string[] arrayval = keyWords.Trim().Split(',');
for (int i = 0; i < arrayval.Length; i++)
{
country = arrayval[i];
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO TestDemot(ClassID, SectionID,CountryId) VALUES(@CID, @SID, @Key)", con))
{
con.Open();
cmd.Parameters.AddWithValue("@CID", "1");
cmd.Parameters.AddWithValue("@SID", "111");
cmd.Parameters.AddWithValue("@Key", country.ToString());
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Country FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
ddlSubjects.DataTextField = "Country"
ddlSubjects.DataValueField = "CustomerId"
ddlSubjects.DataSource = dt
ddlSubjects.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim country As String = String.Empty
Dim keyWords As String = hfSelected.Value
Dim arrayval As String() = keyWords.Trim().Split(","c)
For i As Integer = 0 To arrayval.Length - 1
country = arrayval(i)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO TestDemot(ClassID, SectionID,CountryId) VALUES(@CID, @SID, @Key)", con)
cmd.Parameters.AddWithValue("@ClassID", "1")
cmd.Parameters.AddWithValue("@SectionID", "111")
cmd.Parameters.AddWithValue("@Country", country.ToString())
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub
Screenshot