Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表框中的值添加到数据库_C#_Asp.net_.net_Checkbox_Listbox - Fatal编程技术网

C# 将列表框中的值添加到数据库

C# 将列表框中的值添加到数据库,c#,asp.net,.net,checkbox,listbox,C#,Asp.net,.net,Checkbox,Listbox,我有一个问题,listbox的值没有添加到数据库中 我有一个checkboxlist和listbox,首先我想在listbox中添加所有选中的复选框值,它工作成功,然后我想在按钮单击事件中将来自checkboxlist的listbox数据添加到数据库中,它不工作,所以如何解决这个问题 <div id="contentwrapper" class="contentwrapper"> <div id="validation" class="subconte

我有一个问题,listbox的值没有添加到数据库中

我有一个checkboxlist和listbox,首先我想在listbox中添加所有选中的复选框值,它工作成功,然后我想在按钮单击事件中将来自checkboxlist的listbox数据添加到数据库中,它不工作,所以如何解决这个问题

<div id="contentwrapper" class="contentwrapper">

            <div id="validation" class="subcontent">

                    <form class="stdform stdform2" style="border-top:solid 1px #ddd">

                        <p>
                            <label>Hotel Name</label>
                            <span class="field">
                                <asp:DropDownList ID="ddlHotel" runat="server">
                                </asp:DropDownList>
                            </span>
                        </p>

                        <p>
                            <fieldset class="fieldset">
                            <legend class="legend">Facilities</legend>
                                <div>
                                    <asp:CheckBoxList ID="cblFacility" runat="server" DataTextField="FacilityName" DataValueField="FacilityID" TextAlign="Right" RepeatColumns="5">
                                    </asp:CheckBoxList>

                                    <div class="clear">
                                    </div>
                                </div>

                            </fieldset>
                        </p>
                        <p  class="stdformbutton">
                            <asp:Button ID="btnAdd" runat="server" CssClass="radius2" Text="Add" onclick="btnAdd_Click" />
                        </p>                            
                    </form>                        
            </div><!--subcontent-->

        </div><!--contentwrapper-->            
        <div id="Div1" class="contentwrapper">              
            <div id="Div2" class="subcontent">
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                    <form class="stdform stdform" style="border-top:solid 1px #ddd">
                        <p>                             
                            <span class="field">
                                <asp:ListBox ID="lstFacility" runat="server" SelectionMode="Multiple"></asp:ListBox><br />
                            </span>
                        </p>                            
                        <p  class="stdformbutton">
                            <asp:Button ID="btnSubmit" runat="server" CssClass="submit radius2" Text="Submit" onclick="btnSubmit_Click" />
                        </p>
                    </form>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>
            </div><!--subcontent-->            
        </div>


酒店名称

设施


和.cs文件是:

protected void Page_Load(object sender, EventArgs e)
    {
        string myConnectionString = "my connection string";

        if (Session["admin"] != null)
        {
            lblEmail.Text = Session["adminEmail"].ToString();
            lblAdmin.Text = "Wel Come " + Session["admin"].ToString();
            lblAdmin1.Text = "Wel Come " + Session["admin"].ToString();
        }
        else
        {
            Response.Redirect("Login.aspx");
        }

        if (!Page.IsPostBack)
        {
            if (Session["hotelID"] != null)
            {
                ddlHotel.SelectedValue = Session["hotelID"].ToString();
            }
            ddlHotel.DataSource = dalMST_Hotel.SelectAll(myConnectionString);
            ddlHotel.DataTextField = "HotelName";
            ddlHotel.DataValueField = "HotelID";
            ddlHotel.DataBind();
            ddlHotel.Items.Insert(0, "Select Hotel");

            BindData();
        }
    }


    private void BindData()
    {
        string myConnectionString = "my connection string";    
        cblFacility.DataSource = dalMST_Facility.SelectAll(myConnectionString);
        cblFacility.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string s1 = string.Empty;
        lstFacility.Items.Clear();
        foreach (ListItem item in this.cblFacility.Items)
        {
            if (item.Selected)
            {
                lstFacility.Items.Add(item);
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string myConnectionString = "my connection string";

        Page.Validate();
        if (Page.IsValid)
        {
            DataTable dt = dalMST_FacilityTran.SelectAll(myConnectionString);
            int cnt = dt.Rows.Count;
            entMST_FacilityTran.HotelID = Convert.ToInt32(ddlHotel.SelectedValue);
            entMST_FacilityTran.FacilityID = 0;

            entMST_FacilityTran.Created = DateTime.Now;
            entMST_FacilityTran.Modified = DateTime.Now;

            #region Insert,Update

            for (int i = 0; i < lstFacility.Items.Count; i++)
            {
                int flag = 0;
                for (int j = 0; j < cnt; j++)
                {
                    int hotelid = Convert.ToInt32(dt.Rows[j][2].ToString());
                    int facilityid = Convert.ToInt32(dt.Rows[j][1].ToString());


                    if (lstFacility.Items[i].Selected)
                    {
                        entMST_FacilityTran.FacilityID = Convert.ToInt32(lstFacility.Items[i].Value);

                        if (entMST_FacilityTran.HotelID == hotelid && entMST_FacilityTran.FacilityID == facilityid)
                        {
                            flag = 1;
                            break;
                        }
                        else
                        {
                            flag = 0;
                        }
                    }
                }
                if (flag == 0)
                {
                    if (dalMST_FacilityTran.Insert(entMST_FacilityTran, myConnectionString))
                    {
                        //txtFacility.Text = "";
                        //Response.Redirect("AddFacility.aspx");

                        //return;
                    }
                }
            }
            Response.Redirect("AddRoomCategory.aspx");


            #endregion
        }
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
string myConnectionString=“我的连接字符串”;
如果(会话[“管理”]!=null)
{
lblEmail.Text=会话[“adminEmail”].ToString();
lblAdmin.Text=“Wel Come”+会话[“admin”].ToString();
lblAdmin1.Text=“Wel Come”+会话[“admin”].ToString();
}
其他的
{
重定向(“Login.aspx”);
}
如果(!Page.IsPostBack)
{
如果(会话[“hotelID”]!=null)
{
ddlHotel.SelectedValue=Session[“hotelID”].ToString();
}
ddlHotel.DataSource=dalMST_Hotel.SelectAll(myConnectionString);
ddlHotel.DataTextField=“HotelName”;
ddlHotel.DataValueField=“HotelID”;
ddlHotel.DataBind();
ddlHotel.Items.插入(0,“选择酒店”);
BindData();
}
}
私有void BindData()
{
string myConnectionString=“我的连接字符串”;
cblFacility.DataSource=dalMST_Facility.SelectAll(myConnectionString);
cblFacility.DataBind();
}
受保护的无效btnAdd_单击(对象发送者,事件参数e)
{
string s1=string.Empty;
lstfility.Items.Clear();
foreach(此.cblFacility.Items中的ListItem项)
{
如果(选定项)
{
LST设施.项目.添加(项目);
}
}
}
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
string myConnectionString=“我的连接字符串”;
Page.Validate();
如果(第页有效)
{
DataTable dt=dalMST_FacilityTran.SelectAll(myConnectionString);
int cnt=dt.Rows.Count;
entMST_FacilityTran.HotelID=Convert.ToInt32(ddlHotel.SelectedValue);
entMST_FacilityTran.FacilityID=0;
entMST_FacilityTran.Created=DateTime.Now;
entMST_FacilityTran.Modified=DateTime.Now;
#区域插入,更新
对于(int i=0;i
首先必须删除嵌套For循环

 String lstName;

 for (int i= 0; i< listBoxEmployeeName.Items.Count;i++)
    {

    lstName=listBoxEmployeeName.Items[i].Text;//Here your value stored in lstName
    //here continue you insert query  

    }
字符串名称;
对于(int i=0;i
存在与更新面板相关的问题

使用以下代码:

 <Triggers>
      <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
      <asp:PostBackTrigger ControlID="btnSubmit" /> 
 </Triggers>

所以提交按钮cilck事件将触发


谢谢

您好,您有任何错误吗?没有,没有任何错误,但记录未插入数据库。请使用“提交”按钮上的“尝试并捕获”块。所以如果有错误,你可以得到错误。你的代码看起来很好。谢谢hitesh,在这段代码中没有触发click事件。光标不会转到