locked
ChecklistBox and Radiobutton selections RRS feed

  • Question

  • User-726880101 posted

    Good day 

    I have a checklistbox and radiobutton on my webform , the data is submitted to a SQL Database , when the data is submitted and i do a select * from , the one entry i have created on the web form create multiple rows withing sql , why is that  and how can i fix the issue. when we complete the form it should only create one row in the sql table 

    Wednesday, December 5, 2018 10:15 AM

All replies

  • User-1716253493 posted

    Collect checked values into a string with comma delimited, then save the data after it.

    string str;
    foreach(){
     //str += ',' + selected 
    }
    //save here

    Thursday, December 6, 2018 12:56 AM
  • User283571144 posted

    Hi Jasonkwp,

    According to your description ,it seems that you have submitted multiple data to SQL database.

    Could you please post more details about your aspx and code-behind code?

     If you could post more details information, it will be more easily for us to reproduce the issue and find out the solution.

    Best Regards,

    Brando

    Thursday, December 6, 2018 5:47 AM
  • User-726880101 posted

    Hi oned_gk here is my code , where do i insert your solution. Also note that each user have many applications , so when a user completes the form they will select multiple applications they use on a daily basis, so when the data goes to the database the application names must be in once cell and the data in one row for the particular user. Besides sending the data to the SQL database , can i also at the same time allow the data to be sent to an email address as well 

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Linq;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Configuration;
    using System.Dynamic;
    using System.Data.SqlClient;
    using System.Data;
    using System.Web.UI.WebControls;

    public partial class NewEmployeeRequestForm : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection conn = new SqlConnection();


    protected void Page_Load(object sender, EventArgs e)

    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    CleartextBoxes(this);
    }


    public void CleartextBoxes(Control parent)
    {

    foreach (Control x in parent.Controls)
    {
    if ((x.GetType() == typeof(TextBox)))
    {

    ((TextBox)(x)).Text = "";
    }

    if (x.HasControls())
    {
    CleartextBoxes(x);
    }
    {
    SqlConnection con = new SqlConnection(@"Data Source=SH-JASONK\DEV;Initial Catalog=EmployeeRegistration;Integrated Security=True");
    {

    con.Open();
    SqlCommand cmd = new SqlCommand("insert into Employees(FirstName,LastName,Email,Gender,Phone,StartDate,Company,Department,Applications,Devices,Requestedby) values(@FirstName, @LastName, @Email, @Gender, @Phone, @StartDate, @Company, @Department, @Applications,@Devices, @Requestedby)", con);

    cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);
    cmd.Parameters.AddWithValue("@LastName", LastName.Text);
    cmd.Parameters.AddWithValue("@Email", Email.Text);
    cmd.Parameters.AddWithValue("@Gender", RadioButtonList.SelectedValue);
    cmd.Parameters.AddWithValue("@Phone", Phone.Text);
    cmd.Parameters.AddWithValue("@StartDate", StartDate.Text);
    cmd.Parameters.AddWithValue("@Company", DropDownList_Company.SelectedValue);
    cmd.Parameters.AddWithValue("@Department", DropDownlist_Departments.SelectedValue);
    cmd.Parameters.AddWithValue("@Applications", Business_Applications.SelectedValue);
    cmd.Parameters.AddWithValue("@Devices", RadioButtonList1.SelectedValue);
    cmd.Parameters.AddWithValue("@Requestedby", Requestedby.Text);

    cmd.ExecuteNonQuery();
    con.Close();

    }
    }
    }
    }


    }

    aspx code:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NewEmployeeRequestForm.aspx.cs" Inherits="NewEmployeeRequestForm" %>

    <!DOCTYPE html>
    <script runat="server">

    </script>

    <h1>New Employee Request Form</h1>
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">
    <title>New Employee Request Form</title>

    <style type="text/css">
    .auto-style1 {
    width: 800px;
    }
    .auto-style2 {
    width: 595px;
    }

    div {
    display: block;
    font-family: 'Century Gothic';
    font-size: small;
    font-weight:bold;

    }
    tr {
    font-family: 'Century Gothic';
    font-size: small;
    font-weight:bold;

    }
    h1 {
    font-family: 'Century Gothic';
    font-weight:bold;

    }

    .auto-style4 {
    width: 360px;
    }

    .auto-style5 {
    width: 470px;
    }

    </style>
    </head>
    <body>

    <form id="form1" runat="server">
    <div>
    <table class="auto-style1">
    <tr>
    <td class="auto-style5">FirstName :</td>
    <td class="auto-style4">
    <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
    </td>

    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>


    <tr>
    <td class="auto-style5">LastName :</td>
    <td class="auto-style4"> <asp:TextBox ID="LastName" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>



    <tr>
    <td class="auto-style5">Gender</td>
    <td class="auto-style4">
    <asp:RadioButtonList ID="RadioButtonList" runat="server">
    <asp:ListItem>Male</asp:ListItem>
    <asp:ListItem>Female</asp:ListItem>
    </asp:RadioButtonList>
    </td>
    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>


    <tr>
    <td class="auto-style5">Email :</td>
    <td class="auto-style4">
    <asp:TextBox ID="Email" runat="server" TextMode="Email"></asp:TextBox>
    </td>
    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>


    <tr>
    <td class="auto-style5">Phone :</td>
    <td class="auto-style4"> <asp:TextBox ID="Phone" runat="server" TextMode="Phone"></asp:TextBox></td>
    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>

    <tr>
    <td class="auto-style5">StartDate :</td>

    <td class="auto-style2">
    <asp:TextBox ID="StartDate" runat="server"></asp:TextBox>
    </tr>

    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>


    <tr>
    <td class="auto-style5">Company</td>

    <td class="auto-style4"><asp:DropDownList ID="DropDownList_Company" Name="Company" ProperyName="SelectedValue" runat="server">
    <asp:ListItem Text="Select Company" Value="select" Selected="True"></asp:ListItem>
    <asp:ListItem Text="Spier Resort Management" Value="Leisure Revenue"></asp:ListItem>
    <asp:ListItem Text="Spier Holdings" Value="Banqueting"></asp:ListItem>
    <asp:ListItem Text="Spier Farms" Value="Leisure Finance"></asp:ListItem>
    <asp:ListItem Text="Spier Wines" Value="Hotel Reception"></asp:ListItem>
    <asp:ListItem Text="Spier Properties" Value="Hotel BackOffice"></asp:ListItem>
    </asp:DropDownList>
    </td>
    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>


    <tr>
    <td class="auto-style5">Department</td>
    <td class="auto-style4">
    <asp:DropDownList ID="DropDownlist_Departments" Name="Departments" PropertyName="SelectedValue" runat="server">
    <asp:ListItem Text="Select Department" Value="select" Selected="True"></asp:ListItem>
    <asp:ListItem Text="Leisure Revenue" Value="Leisure Revenue"></asp:ListItem>
    <asp:ListItem Text="Banqueting" Value="Banqueting"></asp:ListItem>
    <asp:ListItem Text="Leisure Finance" Value="Leisure Finance"></asp:ListItem>
    <asp:ListItem Text="Hotel Reception" Value="Hotel Reception"></asp:ListItem>
    <asp:ListItem Text="Hotel BackOffice" Value="Hotel BackOffice"></asp:ListItem>
    <asp:ListItem Text="Hotel Front Office" Value="Hotel Front Office"></asp:ListItem>
    <asp:ListItem Text="Hotel Kitchen" Value="Hotel Kitchen"></asp:ListItem>
    <asp:ListItem Text="Housekeeping" Value="Housekeeping"></asp:ListItem>
    <asp:ListItem Text="Leisure Stores" Value="Leisure Stores"></asp:ListItem>
    <asp:ListItem Text="Eight Restaurant" Value="Eight Restaurant"></asp:ListItem>
    <asp:ListItem Text="Banqueting" Value="Banqueting"></asp:ListItem>
    <asp:ListItem Text="Human Resources" Value="Human Resources"></asp:ListItem>
    <asp:ListItem Text="Spier Farm Kitchen" Value="Spier Farm Kitchen"></asp:ListItem>
    <asp:ListItem Text="Leisure Properties & Maintenance" Value="Leisure Properties & Maintenance"></asp:ListItem>
    <asp:ListItem Text="Spier Farms" Value="Spier Farms"></asp:ListItem>
    <asp:ListItem Text="SW Sales" Value="SW Sales"></asp:ListItem>
    <asp:ListItem Text="SW Marketing" Value="SW Marketing"></asp:ListItem>
    <asp:ListItem Text="SW Logistics" Value="SW Logistics"></asp:ListItem>
    <asp:ListItem Text="SW Winemaking" Value="SW Winemaking"></asp:ListItem>
    <asp:ListItem Text="SW Winetasting" Value="SW Winetasting"></asp:ListItem>
    <asp:ListItem Text="SW Cellar" Value="SW Cellar"></asp:ListItem>
    <asp:ListItem Text="SW Maintenance" Value="SW Maintenance"></asp:ListItem>
    <asp:ListItem Text="SW Quality" Value="SW Quality"></asp:ListItem>
    <asp:ListItem Text="SW Farms" Value="SW Farms"></asp:ListItem>
    </asp:DropDownList>
    </td>
    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5">Business Applications</td>

    <td>
    <asp:CheckBoxList ID="Business_Applications" runat="server">
    <asp:ListItem Value="Microsoft Dynamics Navision"> Microsoft Dynamics Navision </asp:ListItem>
    <asp:ListItem Value="WineMS"> WineMS </asp:ListItem>
    <asp:ListItem Value="CPAR"> CPAR </asp:ListItem>
    <asp:ListItem Value="Micros POS"> Micros POS </asp:ListItem>
    <asp:ListItem Value="Opera PMS"> Opera PMS </asp:ListItem>
    <asp:ListItem Value="Micros EMC"> Micros EMC </asp:ListItem>
    <asp:ListItem Value="Materials Control"> Materials Control </asp:ListItem>
    <asp:ListItem Value="Sage Accpac"> Sage Accpac </asp:ListItem>
    <asp:ListItem Value="Jet Professional"> Jet Professional </asp:ListItem>
    </asp:CheckBoxList></td>

    </tr>




    <tr>
    <td class="auto-style5">Devices</td>
    <td class="auto-style4">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem>Destop</asp:ListItem>
    <asp:ListItem>Laptop</asp:ListItem>
    </asp:RadioButtonList>
    </td>
    </tr>



    <tr>
    <td class="auto-style5">Requestedby</td>
    <td class="auto-style4">
    <asp:TextBox ID="Requestedby" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>
    <tr><td class="auto-style5"></td></tr>


    </tr>
    <tr>
    <td class="auto-style5">

    <asp:Button ID="Button1" runat="server" Text="SUBMIT" OnClick="Button1_Click" />
    </td>
    <tr>
    <td class="auto-style5">
    <asp:Literal ID="Lit1" runat="server"></asp:Literal>
    </td>
    </tr>

    </table>
    </div>
    </form>
    </body>
    </html>

    Thursday, December 6, 2018 6:58 AM
  • User-726880101 posted

    Here is my code 

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Linq;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Configuration;
    using System.Dynamic;
    using System.Data.SqlClient;
    using System.Data;
    using System.Web.UI.WebControls;

    public partial class NewEmployeeRequestForm : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection conn = new SqlConnection();


    protected void Page_Load(object sender, EventArgs e)

    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    CleartextBoxes(this);
    }


    public void CleartextBoxes(Control parent)
    {

    foreach (Control x in parent.Controls)
    {
    if ((x.GetType() == typeof(TextBox)))
    {

    ((TextBox)(x)).Text = "";
    }

    if (x.HasControls())
    {
    CleartextBoxes(x);
    }
    {
    SqlConnection con = new SqlConnection(@"Data Source=SH-JASONK\DEV;Initial Catalog=EmployeeRegistration;Integrated Security=True");
    {

    con.Open();
    SqlCommand cmd = new SqlCommand("insert into Employees(FirstName,LastName,Email,Gender,Phone,StartDate,Company,Department,Applications,Devices,Requestedby) values(@FirstName, @LastName, @Email, @Gender, @Phone, @StartDate, @Company, @Department, @Applications,@Devices, @Requestedby)", con);

    cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);
    cmd.Parameters.AddWithValue("@LastName", LastName.Text);
    cmd.Parameters.AddWithValue("@Email", Email.Text);
    cmd.Parameters.AddWithValue("@Gender", RadioButtonList.SelectedValue);
    cmd.Parameters.AddWithValue("@Phone", Phone.Text);
    cmd.Parameters.AddWithValue("@StartDate", StartDate.Text);
    cmd.Parameters.AddWithValue("@Company", DropDownList_Company.SelectedValue);
    cmd.Parameters.AddWithValue("@Department", DropDownlist_Departments.SelectedValue);
    cmd.Parameters.AddWithValue("@Applications", Business_Applications.SelectedValue);
    cmd.Parameters.AddWithValue("@Devices", RadioButtonList1.SelectedValue);
    cmd.Parameters.AddWithValue("@Requestedby", Requestedby.Text);

    cmd.ExecuteNonQuery();
    con.Close();

    }
    }
    }
    }


    }

    Thursday, December 6, 2018 12:51 PM
  • User283571144 posted

    Hi Jasonkwp,

    According to the code you posted, you could write the sql code outside the foreach method,else whenever it chooses a control in a page,the sql method will be executed. Besides, if you want to upload all checked application into one cell,you could use List to get value of checkboxs you selected and add it into the sql parameter.

    Here is the code,I hope it will help you.

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
               
               <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
               
                <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                   <asp:ListItem Value="WineMS"> WineMS </asp:ListItem>
          <asp:ListItem Value="CPAR"> CPAR </asp:ListItem>
         <asp:ListItem Value="Micros POS"> Micros POS </asp:ListItem>
          <asp:ListItem Value="Opera PMS"> Opera PMS </asp:ListItem>
                </asp:CheckBoxList>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server"> 
    <asp:ListItem>Male</asp:ListItem> 
    <asp:ListItem>Female</asp:ListItem> 
    </asp:RadioButtonList> 
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    

    Code-behind:

        protected void Button1_Click(object sender, EventArgs e)
            {
                CleartextBoxes(this);
            }
            public void CleartextBoxes(Control parent)
            {
                List<string> selectedValues = CheckBoxList1.Items.Cast<ListItem>().
                    Where(li => li.Selected)
                    .Select(li => li.Value)
                    .ToList();
                string s = "";
                for (int i = 0; i < selectedValues.Count; i++)
                {
                    s += selectedValues[i]+"  ";
                } //Get the value of  the checkbox you selected in checkboxlist
                foreach (Control x in parent.Controls)
                {
    
                    if ((x.GetType() == typeof(TextBox)))
                    {
    
                        ((TextBox)(x)).Text = "";
                    }
    
    
                }
                string conStr = @"Data Source=localhost;Initial Catalog=MyDatabase2;Integrated Security=True";
                string sql = "insert into Employees values(@FirstName,@LastName,@Gender)";
                using (SqlConnection con = new SqlConnection(conStr))
                {
    
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
    
                        con.Open();
                        cmd.Parameters.AddWithValue("@FirstName", TextBox1.Text);
                        cmd.Parameters.AddWithValue("@LastName",s);
                        cmd.Parameters.AddWithValue("@Gender", RadioButtonList1.SelectedItem.ToString());
                        cmd.ExecuteNonQuery();
    
                    }
                   
    
    
                }
    
    
            } 
    

    ResulT:

    Best Regards,

    Brando

    Wednesday, December 12, 2018 6:33 AM