亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將多個選定的列表框項目插入到 SQL 表中的同一個單元格中

將多個選定的列表框項目插入到 SQL 表中的同一個單元格中

C#
慕容3067478 2021-11-28 16:18:25
我想將多個列表框項目插入到 SQL 表中的一個單元格中,并用逗號分隔項目。下面發布的代碼只會添加列表框中的第一個選定項目。因此,如果您選擇 2 或 10 個項目,您選擇的第一個項目將被插入到表格中。for 循環是我的問題,我需要獲取所有選定的值。謝謝        protected void pg_upload_Click(object sender, EventArgs e)        {        using (SqlConnection mycon = new SqlConnection(connectionstring))        {            using (SqlCommand mycmd = mycon.CreateCommand())            {                if (textbox_make.Text == string.Empty || textbox_number.Text == string.Empty)                {                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('The Make/Model and Number must be Entered')", true);                }                else                {                    string str = "";                    for (int i=0; i<= listbox_software.Items.Count; i++)                    {                        str = listbox_software.SelectedItem.ToString();                    }                    mycon.Open();                    mycmd.CommandText = "INSERT INTO tbl_PG (Model, PGNumber, AssetNo, Area, Owner,IPAddress, SerialNo, OSVersion, Memory, Software) " +                                        "Values ('" + textbox_make.Text + "' , '" + textbox_number.Text + "' , '" + textbox_asset.Text + "' , '" + drop_area.Text + "' , '" + drop_owner.Text + "' , '" + textbox_ip.Text + "' " +                                                ", '" + textbox_serial.Text + "' , '" + textbox_os.Text + "' , '" + textbox_memory.Text + "' ,  '" + str + "')";                    mycmd.ExecuteNonQuery();                    PopulateGridView();                    lblsuscessmessage.Text = "Selected Record Added";                    lblerrormessage.Text = "";                }            }        }    }
查看完整描述

2 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

添加以下命名空間:

using System.Linq;

創建所選項目的字符串數組,然后使用 string.join:

    var selection = listbox_software.SelectedItems
        .Cast<string>()
        .ToArray();
    var str = string.Join(",", selection);


查看完整回答
反對 回復 2021-11-28
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

我找到了答案。


                        // To access checkbox list item's value //

                    string total = "";

                    foreach (ListItem listItem in listbox_software.Items)

                    {

                        if (listItem.Selected)

                        {

                            total =  total + "[" + listItem.Value + "][ " + " ";

                        }

                    }

                    string str = total.ToString();


查看完整回答
反對 回復 2021-11-28
  • 2 回答
  • 0 關注
  • 237 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號