Hi all i need to add multiple selected checkboxlist items to one row under the applications column, e.g. Business Central, Opera PMS, Micros. I am struggling to be cable to figure out the script to write.
Below is my aspx code:
<td class="auto-style15">
<asp:CheckBoxList ID="AppList" runat="server" Height="60px" RepeatColumns="3" RepeatDirection="Horizontal" Width="486px">
<asp:ListItem>Business Central</asp:ListItem>
<asp:ListItem>WineMS</asp:ListItem>
<asp:ListItem>CPAR</asp:ListItem>
<asp:ListItem>OperaPMS</asp:ListItem>
<asp:ListItem>My Micos</asp:ListItem>
<asp:ListItem>Micros EMC</asp:ListItem>
<asp:ListItem>Jet Professional</asp:ListItem>
</asp:CheckBoxList>
</td>
Here is my C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace EmployeeRequest
{
public partial class EmployeeRequestForm : System.Web.UI.Page
{
string ConnectionString = @"Data Source=sh-jasonkNew\ICTDev;Initial Catalog=NewUserReqForm;Integrated Security=True;";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtName.Text == "" || txtSurname.Text == "" || txtDepartment.Text == "")
lblErrorMessage.Text = "Please fill Mandatory Fields";
using (SqlConnection sqlCon = new SqlConnection(ConnectionString))
{
sqlCon.Open();
SqlCommand SqlCmd = new SqlCommand("EmployeeAdd", sqlCon);
SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
SqlCmd.Parameters.AddWithValue("@Surname", txtSurname.Text.Trim());
SqlCmd.Parameters.AddWithValue("@Department", txtDepartment.Text.Trim());
SqlCmd.Parameters.AddWithValue("@EmploymentType", ddlEmploymentType.Text.Trim());
SqlCmd.Parameters.AddWithValue("@CommenceDate", DateTime.Now);
SqlCmd.Parameters.AddWithValue("@JobTitle", txtJobTitle.Text.Trim());
SqlCmd.Parameters.AddWithValue("@TelephoneExt", txtTelephoneExt.Text.Trim());
SqlCmd.Parameters.AddWithValue("@Gender", ddlGender.Text.Trim());
SqlCmd.Parameters.AddWithValue("@Workstations", ddlWorkstations.Text.Trim());
SqlCmd.Parameters.AddWithValue("@Applications", AppList.Text.Trim());
SqlCmd.Parameters.AddWithValue("@FolderAccessRights", txtFolderAccessRights.Text.Trim());
SqlCmd.Parameters.AddWithValue("@EmailGroupAccess", txtEmailGroupRights.Text.Trim());
SqlCmd.Parameters.AddWithValue("@Authorisedby", txtAuth.Text.Trim());
SqlCmd.ExecuteNonQuery();
Clear();
lblSuccessMessage.Text = "Information Submitted Successfully";
}
}
void Clear()
{
txtName.Text = txtSurname.Text = txtDepartment.Text = Cal1.Text = ddlEmploymentType.Text = txtJobTitle.Text = txtTelephoneExt.Text = ddlGender.Text = txtFolderAccessRights.Text = txtEmailGroupRights.Text = txtAuth.Text ="";
hfEmployeeID.Value = "";
lblSuccessMessage.Text = lblErrorMessage.Text = "";
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string chkboxselect = "";
for (int i=0;i<AppList.Items.Count;i++)
{
if(AppList.Items[i].Selected)
{
if(chkboxselect == "")
{
chkboxselect = AppList.Items[i].Text;
}
else
{
chkboxselect += "," + AppList.Items[i].Text;
}
}
}
string mainconn = ConfigurationManager.ConnectionStrings["NewUserReqFormConnectionString1"].ConnectionString;
SqlCommand sqlconn = new SqlCommand(mainconn);
string sqlquery = "insert into [dbo].[tbl_EmployeeAdd] ([Applications]) values ('"+ chkboxselect + "')";
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
}
}
}
What I have tried:
I have tried the following script but it still does not add the application names into the column. what it does it only add the abbreviation of the first selected checkbox e.g. Bus or Opr, Mic etc.
string chkboxselect = "";
for (int i=0;i<AppList.Items.Count;i++)
{
if(AppList.Items[i].Selected)
{
if(chkboxselect == "")
{
chkboxselect = AppList.Items[i].Text;
}
else
{
chkboxselect += "," + AppList.Items[i].Text;
}
}
}
string mainconn = ConfigurationManager.ConnectionStrings["NewUserReqFormConnectionString1"].ConnectionString;
SqlCommand sqlconn = new SqlCommand(mainconn);
string sqlquery = "insert into [dbo].[tbl_EmployeeAdd] ([Applications]) values ('"+ chkboxselect + "')";
}