[英]How to Find ListBox1.SelectedItem in other ListBox2 Items
[英]Store the ListBox2 items in SQL table
在我的網頁中,我有2個列表框,即ListBox1,ListBox2。用戶從ListBox1中選擇項目列表並將其移動到ListBox2。 我做到了這一點 ,但是在我單擊“保存”按鈕后,它不會保存SQL表中的ListBox2選定項目並且它不會拋出任何錯誤! 如何存放?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblPage1ID.Text=Server.UrlDecode(Request.QueryString["Parameter"].ToString());
ListBoxWorksPackages();
}
}
protected void ListBoxWorksPackages()
{
ListBox1.Items.Add("General Contractor");
ListBox1.Items.Add("Architecture");
ListBox1.Items.Add("Civil");
ListBox1.Items.Add("Mechanical");
ListBox1.Items.Add("Electrical");
}
protected void btnMoveRight1_Click(object sender, EventArgs e)
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListItem li = ListBox1.Items[i];
ListBox1.Items.Remove(li);
}
}
}
protected void btnMoveLeft1_Click(object sender, EventArgs e)
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected == true)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListItem li = ListBox2.Items[i];
ListBox2.Items.Remove(li);
}
}
}
protected void BtnSave1_Click(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(GetConnectionString());
string Packagevalues = string.Empty;
foreach (ListItem item in ListBox2.Items)
{
if (item.Selected == true)
{
Packagevalues += "," + item.Text;
}
}
string query = "INSERT INTO Contractor_Info2 (Vendor_ID,WorksPackage) VALUES"
+ "(@Vendor_ID,@WorksPackage )";
try
{
SqlCommand cmd = new SqlCommand(query, SqlCon);
cmd.CommandType = CommandType.Text;
SqlCon.Open();
cmd.Parameters.AddWithValue("@Vendor_ID", lblPage1ID.Text);
cmd.Parameters.AddWithValue("@WorksPackage", Packagevalues);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
SqlCon.Close();
}
}
在將參數添加到sql過程后,需要調用cmd.ExecuteNonQuery()
。 這是實際運行sql語句的內容。
try
{
SqlCommand cmd = new SqlCommand(query, SqlCon);
cmd.CommandType = CommandType.Text;
SqlCon.Open();
cmd.Parameters.AddWithValue("@Vendor_ID", lblPage1ID.Text);
cmd.Parameters.AddWithValue("@WorksPackage", Packagevalues);
// add this
cmd.ExecuteNonQuery();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.