title: add values from listbox to database
body: <p>I have a problem that the value of listbox don't add into the database.</p>
  
  <p>i have a checkboxlist and listbox, first i want to add all selected checkbox value in listbox, it works successfuly, and then i want to add that data of listbox come form checkboxlist to database on button click event, it do not work so how to solve this.</p>
  
  <pre><code>&lt;div id="contentwrapper" class="contentwrapper"&gt;
  
              &lt;div id="validation" class="subcontent"&gt;
  
                      &lt;form class="stdform stdform2" style="border-top:solid 1px #ddd"&gt;
  
                          &lt;p&gt;
                              &lt;label&gt;Hotel Name&lt;/label&gt;
                              &lt;span class="field"&gt;
                                  &lt;asp:DropDownList ID="ddlHotel" runat="server"&gt;
                                  &lt;/asp:DropDownList&gt;
                              &lt;/span&gt;
                          &lt;/p&gt;
  
                          &lt;p&gt;
                              &lt;fieldset class="fieldset"&gt;
                              &lt;legend class="legend"&gt;Facilities&lt;/legend&gt;
                                  &lt;div&gt;
                                      &lt;asp:CheckBoxList ID="cblFacility" runat="server" DataTextField="FacilityName" DataValueField="FacilityID" TextAlign="Right" RepeatColumns="5"&gt;
                                      &lt;/asp:CheckBoxList&gt;
  
                                      &lt;div class="clear"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                              &lt;/fieldset&gt;
                          &lt;/p&gt;
                          &lt;p  class="stdformbutton"&gt;
                              &lt;asp:Button ID="btnAdd" runat="server" CssClass="radius2" Text="Add" onclick="btnAdd_Click" /&gt;
                          &lt;/p&gt;                            
                      &lt;/form&gt;                        
              &lt;/div&gt;&lt;!--subcontent--&gt;
  
          &lt;/div&gt;&lt;!--contentwrapper--&gt;            
          &lt;div id="Div1" class="contentwrapper"&gt;              
              &lt;div id="Div2" class="subcontent"&gt;
                  &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
                      &lt;ContentTemplate&gt;
                      &lt;form class="stdform stdform" style="border-top:solid 1px #ddd"&gt;
                          &lt;p&gt;                             
                              &lt;span class="field"&gt;
                                  &lt;asp:ListBox ID="lstFacility" runat="server" SelectionMode="Multiple"&gt;&lt;/asp:ListBox&gt;&lt;br /&gt;
                              &lt;/span&gt;
                          &lt;/p&gt;                            
                          &lt;p  class="stdformbutton"&gt;
                              &lt;asp:Button ID="btnSubmit" runat="server" CssClass="submit radius2" Text="Submit" onclick="btnSubmit_Click" /&gt;
                          &lt;/p&gt;
                      &lt;/form&gt;
                      &lt;/ContentTemplate&gt;
                      &lt;Triggers&gt;
                          &lt;asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" /&gt;
                      &lt;/Triggers&gt;
                  &lt;/asp:UpdatePanel&gt;
              &lt;/div&gt;&lt;!--subcontent--&gt;            
          &lt;/div&gt;
  </code></pre>
  
  <p>AND .cs file is :</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          string myConnectionString = "my connection string";
  
          if (Session["admin"] != null)
          {
              lblEmail.Text = Session["adminEmail"].ToString();
              lblAdmin.Text = "Wel Come " + Session["admin"].ToString();
              lblAdmin1.Text = "Wel Come " + Session["admin"].ToString();
          }
          else
          {
              Response.Redirect("Login.aspx");
          }
  
          if (!Page.IsPostBack)
          {
              if (Session["hotelID"] != null)
              {
                  ddlHotel.SelectedValue = Session["hotelID"].ToString();
              }
              ddlHotel.DataSource = dalMST_Hotel.SelectAll(myConnectionString);
              ddlHotel.DataTextField = "HotelName";
              ddlHotel.DataValueField = "HotelID";
              ddlHotel.DataBind();
              ddlHotel.Items.Insert(0, "Select Hotel");
  
              BindData();
          }
      }
  
  
      private void BindData()
      {
          string myConnectionString = "my connection string";    
          cblFacility.DataSource = dalMST_Facility.SelectAll(myConnectionString);
          cblFacility.DataBind();
      }
  
      protected void btnAdd_Click(object sender, EventArgs e)
      {
          string s1 = string.Empty;
          lstFacility.Items.Clear();
          foreach (ListItem item in this.cblFacility.Items)
          {
              if (item.Selected)
              {
                  lstFacility.Items.Add(item);
              }
          }
      }
  
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
          string myConnectionString = "my connection string";
  
          Page.Validate();
          if (Page.IsValid)
          {
              DataTable dt = dalMST_FacilityTran.SelectAll(myConnectionString);
              int cnt = dt.Rows.Count;
              entMST_FacilityTran.HotelID = Convert.ToInt32(ddlHotel.SelectedValue);
              entMST_FacilityTran.FacilityID = 0;
  
              entMST_FacilityTran.Created = DateTime.Now;
              entMST_FacilityTran.Modified = DateTime.Now;
  
              #region Insert,Update
  
              for (int i = 0; i &lt; lstFacility.Items.Count; i++)
              {
                  int flag = 0;
                  for (int j = 0; j &lt; cnt; j++)
                  {
                      int hotelid = Convert.ToInt32(dt.Rows[j][2].ToString());
                      int facilityid = Convert.ToInt32(dt.Rows[j][1].ToString());
  
  
                      if (lstFacility.Items[i].Selected)
                      {
                          entMST_FacilityTran.FacilityID = Convert.ToInt32(lstFacility.Items[i].Value);
  
                          if (entMST_FacilityTran.HotelID == hotelid &amp;&amp; entMST_FacilityTran.FacilityID == facilityid)
                          {
                              flag = 1;
                              break;
                          }
                          else
                          {
                              flag = 0;
                          }
                      }
                  }
                  if (flag == 0)
                  {
                      if (dalMST_FacilityTran.Insert(entMST_FacilityTran, myConnectionString))
                      {
                          //txtFacility.Text = "";
                          //Response.Redirect("AddFacility.aspx");
  
                          //return;
                      }
                  }
              }
              Response.Redirect("AddRoomCategory.aspx");
  
  
              #endregion
          }
      }
  </code></pre>