This is the codebehind for an asp.net Web form that I am trying to build in Visual Studio. All works great, except I want a listbox that lets me select more then one item and pass it to a SQL database. Right now it only passes one value. I know I have to loop through, but I don’t know where to put that loop in my program. Does anyone have a sample program or can see where in this program to add the Loop.
The one that I am trying to loop through is:
"protected System.Web.UI.WebControls.ListBox CSOoptionsgroups;"
Thanks.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace USO
{
///
/// Summary description for ComputerSOForm.
///
public class ComputerSOForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox IdUni;
protected System.Web.UI.WebControls.TextBox CSOdate;
protected System.Web.UI.WebControls.TextBox CSOemployeenamelast;
protected System.Web.UI.WebControls.TextBox CSOemployeenamefirst;
protected System.Web.UI.WebControls.TextBox CSOemployeenamemiddle;
protected System.Web.UI.WebControls.TextBox CSOssnumber;
protected System.Web.UI.WebControls.TextBox CSOnickname;
protected System.Web.UI.WebControls.DropDownList CSOworktimeuser;
protected System.Web.UI.WebControls.DropDownList CSOprovider;
protected System.Web.UI.WebControls.TextBox CSOpcdevice;
protected System.Web.UI.WebControls.DropDownList CSOlocation;
protected System.Web.UI.WebControls.TextBox CSOdepartment;
protected System.Web.UI.WebControls.TextBox CSOposition;
protected System.Web.UI.WebControls.TextBox CSOdateeffective;
protected System.Web.UI.WebControls.TextBox CSOpersonreplacing;
protected System.Web.UI.WebControls.TextBox CSOmirrorprofile;
protected System.Web.UI.WebControls.CheckBox CSOoptionswww;
protected System.Web.UI.WebControls.CheckBox CSOoptionsbh;
protected System.Web.UI.WebControls.CheckBox CSOoptionscsi;
protected System.Web.UI.WebControls.CheckBox CSOoptionsint;
protected System.Web.UI.WebControls.CheckBox CSOoptionsstm;
protected System.Web.UI.WebControls.CheckBox CSOoptionsstt;
protected System.Web.UI.WebControls.CheckBox CSOoptionsoutlook;
protected System.Web.UI.WebControls.CheckBox CSOoptionsics;
protected System.Web.UI.WebControls.ListBox CSOoptionsgroups;
protected System.Web.UI.WebControls.TextBox CSOemployeexpdate;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Web.UI.WebControls.Button Submit;
private void AddRequest()
{
string strSQLTemp = "INSERT INTO CSOData(IdUni, CSOdate," +
"CSOemployeenamefirst, CSOemployeenamelast, " +
"CSOemployeenamemiddle, CSOnickname, CSOssnumber, " +
"CSOworktimeuser, CSOemployeexpdate, CSOprovider, " +
"CSOpcdevice, CSOlocation, CSOdepartment, " +
"CSOposition, CSOdateeffective, CSOpersonreplacing, " +
"CSOoptionsgroups, CSOmirrorprofile, " +
"CSOoptionswww, CSOoptionsbh,CSOoptionscsi, " +
"CSOoptionsint, CSOoptionsstm, CSOoptionsstt, " +
"CSOoptionsoutlook, CSOoptionsics) " +
"VALUES('" + IdUni.Text + "'," +
"'" + CSOdate.Text + "'," +
"'" + CSOemployeenamefirst.Text + "'," +
"'" + CSOemployeenamelast.Text + "'," +
"'" + CSOemployeenamemiddle.Text + "'," +
"'" + CSOnickname.Text + "'," +
"'" + CSOssnumber.Text + "'," +
"'" + CSOworktimeuser.SelectedItem.Text + "'," +
"'" + CSOemployeexpdate.Text + "'," +
"'" + CSOprovider.SelectedItem.Text + "'," +
"'" + CSOpcdevice.Text + "'," +
"'" + CSOlocation.SelectedItem.Text + "'," +
"'" + CSOdepartment.Text + "'," +
"'" + CSOposition.Text + "'," +
"'" + CSOdateeffective.Text + "'," +
"'" + CSOpersonreplacing.Text + "'," +
"'" + CSOoptionsgroups.SelectedItem.Text + "'," +
"'" + CSOmirrorprofile.Text + "'," +
"'" + CSOoptionswww.Text + "'," +
"'" + CSOoptionsbh.Text + "'," +
"'" + CSOoptionscsi.Text + "'," +
"'" + CSOoptionsint.Text + "'," +
"'" + CSOoptionsstm.Text + "'," +
"'" + CSOoptionsstt.Text + "'," +
"'" + CSOoptionsoutlook.Text + "'," +
"'" + CSOoptionsics.Text + "')";
ExecNonQuery(strSQLTemp);
}
private void ExecNonQuery(string strSQL)
{
SqlCommand cmdCommand = new SqlCommand(strSQL, sqlConnection1);
sqlConnection1.Open();
cmdCommand.CommandType = CommandType.Text;
cmdCommand.ExecuteNonQuery();
sqlConnection1.Close();
}
private void Page_Load(object sender, System.EventArgs e)
{
int intNumber;
Random r = new Random();
intNumber = Convert.ToInt32(r.Next(98)) + 1;;
CSOdate.Text = DateTime.Now.Month.ToString() + "/" +
DateTime.Now.Day.ToString() + "/" +
DateTime.Now.Year.ToString();
IdUni.Text = DateTime.Now.Month.ToString() +
DateTime.Now.Day.ToString() +
DateTime.Now.Year.ToString() +
DateTime.Now.Hour.ToString() +
DateTime.Now.Minute.ToString() +
DateTime.Now.Second.ToString() + intNumber ;
if (!(Page.IsPostBack))
{
String strSQLforListBox = "SELECT * " +
"FROM MailGroup ";
SqlCommand objCommand = new SqlCommand(strSQLforListBox, sqlConnection1);
sqlConnection1.Open();
CSOoptionsgroups.DataSource = objCommand.ExecuteReader();
CSOoptionsgroups.DataTextField = "MGroups";
CSOoptionsgroups.DataBind();
sqlConnection1.Close();
}
if (!(Page.IsPostBack))
{
ArrayList status = new ArrayList (5);
status.Add("");
status.Add("Full Time");
status.Add("Part Time");
status.Add("Per Diem");
status.Add("Seasonal");
CSOworktimeuser.DataSource = status;
CSOworktimeuser.DataBind();
ArrayList provider = new ArrayList (3);
provider.Add("");
provider.Add("Yes");
provider.Add("No");
CSOprovider.DataSource = provider;
CSOprovider.DataBind();
ArrayList org = new ArrayList (4);
org.Add("");
org.Add("Applied Health Services");
org.Add("Brendan House");
org.Add("Kalispell Regional Medical Center");
CSOlocation.DataSource = org;
CSOlocation.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
///
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=CIS-NA1\\NetSDK;initial catalog=CSO;persist security info=False;user i" +
"d=HelpDesk;password=???????;workstation id=CIS-NA1;packet size=4096";
this.Submit.Click += new System.EventHandler(this.Submit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Submit_Click(object sender, System.EventArgs e)
{
AddRequest();
}
}
}