且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在特定日期的数据库中插入复选框值。

更新时间:2022-12-11 20:14:57

如果您想插入值选中复选框到单列(多行),您必须遍历复选框集合并为每个选中的复选框调用插入子例程。因此,在伪代码中:

  foreach (复选框复选框列表中)
{
if (checkbox.Checked)
{
// 在此处插入子程序!
}
}





如果您希望将值存储在单个列(单行)中作为逗号分隔字符串( value1,value2等)你必须遍历复选框的集合并将已检查复选框的值提取到字符串中并在最后调用insert子例程。在伪代码中:

 StringBuilder sb =  new  StringBuilder(); 
foreach (复选框复选框列表中)
{
if (checkbox.Checked)
{
sb.Add(checkbox.Value + ,);
}
}
// 此处插入子程序


I ddont want to use checkboxlist because in checkboxlist all value insert in single column , i am using 6 checkbox , if we select 2-3 checkbox in any date, 2-3 value should be insert in database

What I have tried:

Design :-

                     <table class="table">
                    <tr><td> <asp:CheckBox ID="CheckBox1" runat="server"   AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged"  />Select All</td> </tr>
                            <tr><td>
                                <asp:CheckBox ID="CheckBox2" runat="server"  Text="12:45PM"/></td> </tr>

                         <tr><td>   <asp:CheckBox ID="CheckBox3" runat="server"  Text="1:00PM" /></td>  </tr>
        
                         <tr><td>   <asp:CheckBox ID="CheckBox4" runat="server"  Text="1:15PM" /></td> </tr>

                         <tr><td>   <asp:CheckBox ID="CheckBox5" runat="server"   Text="6:00PM"/></td> </tr>

                         <tr><td>   <asp:CheckBox ID="CheckBox6" runat="server"   Text="6:15PM"/></td></tr>
                 
                         <tr><td>   <asp:CheckBox ID="CheckBox7" runat="server"  Text="6:30PM" /></td></tr>



<tr><td>  
    <asp:Button ID="btnblock" runat="server" Text="Block" Height="34px" Width="74px" OnClick="btnblock_Click" /></td></tr>  </table>
                    </div>
             </div>         
    </div>




code:

{
    using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    using (SqlCommand cmm = new SqlCommand())
    {
        cmm.Connection = cnn;
        cmm.CommandType = CommandType.Text;
        cmm.CommandText = "INSERT INTO[consult_slotblock]([blockdate],[slotime])" + "VALUES(@blockdate,@slotime)";
        cmm.Parameters.AddWithValue("@blockdate", TextBox1.Text.Trim());


        if (CheckBox2.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox2.Text);

        }
          if (CheckBox3.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox3.Text);

        }
         if (CheckBox4.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox4.Text);

        }
         if (CheckBox5.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox5.Text);

        }
         if (CheckBox6.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox6.Text);

        }

         if (CheckBox7.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox7.Text);

        }

        cnn.Open();
        cmm.ExecuteNonQuery();
        ScriptManager.RegisterStartupScript(this, GetType(), "Success", "alert('Information Save Successfully.');", true);
        cnn.Close();

    }
}

If you would like to insert values of checked checkboxes into single column (multiple rows) you have to loop through the collection of checkboxes and call insert subroutine for every checked checkbox. So, in pseudo-code:
foreach(checkbox in checkboxlist)
{
    if (checkbox.Checked)
    {
        //insert subroutine here!
    }
}



In case, you want to store values in a single column (single row) as a comma seperated string ("value1,value2,etc") you have to loop through the collection of checkboxes and fetch values of checked checkboxes into string and call insert subroutine at the end. In pseudo-code:

StringBuilder sb =new StringBuilder();
foreach(checkbox in checkboxlist)
{
    if (checkbox.Checked)
    {
        sb.Add(checkbox.Value + ",");
    }
}
//here insert subroutine